简体   繁体   English

在目标 c 中组合两个 NSMutableArray

[英]Combine Two NSMutableArray In Objective c

I have Two NSMutableArray Array I want to combine this array in to old one array.我有两个 NSMutableArray 数组我想将这个数组组合成一个旧数组。

How can i do this without any loop (for, while).我怎么能在没有任何循环(for,while)的情况下做到这一点。

         NSMutableArray *oldArray = [[NSMutableArray alloc]init];
         [oldArray addObject:@"four"];
         [oldArray addObject:@"five"];
         [oldArray addObject:@"six"];

         NSMutableArray *newArray = [[NSMutableArray alloc]init];
         [newArray addObject:@"one"];
         [newArray addObject:@"two"];
         [newArray addObject:@"three"];

After Combine I want output like this:合并后我想要 output 像这样:

         NSLog(@"After Combine : %@",oldArray);
         //Output : one, two, three, four, five, six

Insert newArray in oldArray :oldArray中插入newArray

[oldArray insertObjects:newArray atIndexes:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, newArray.count)]]

You can use addObjectesFromArray to get results您可以使用addObjectesFromArray来获取结果

 [oldArray addObjectsFromArray: newArray];

This should do it.这应该这样做。

newArray = [[newArray arrayByAddingObjectsFromArray:oldArray] mutableCopy];

for (NSString *str in newArray) {
    NSLog(@"%@", str);
}

output: [one,two,three,four,five,six] output: [one,two,three,four,five,six]

wait, thats was copying into the new one.. :)等等,那是复制到新的.. :)

 oldArray = [[newArray arrayByAddingObjectsFromArray:oldArray] mutableCopy];

output: [one,two,three,four,five,six] output: [one,two,three,four,five,six]

hahaha, possibly with the side effect that newArray is NSArray and has same content.哈哈哈,可能副作用是 newArray 是 NSArray 并且具有相同的内容。
Check?查看? Oh.哦。 newArray is still [one,two,three] . newArray仍然是[one,two,three]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM