macos 将一个 NSMutableArray 连接到另一个 NSMutableArray 的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3549060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate one NSMutableArray to the end of another NSMutableArray
提问by Eric Brotto
A simple answer to this super simple question would be great! Here is the pseudcode:
这个超级简单的问题的简单答案会很棒!这是伪代码:
NSMutableArray *Africa = [Lion, Tiger, Zebra];
NSMutableArray *Canada = [Polar Bear, Beaver , Loon];
NSMutableArray *Animals = *Africa + *Canada;
What I want to end up with:
我想最终得到什么:
Animals = [Lion, Tiger, Zebra, Polar Bear, Beaver, Loon];
What is the proper syntax to achieve this in Objective-C/ Cocoa?
在 Objective-C/Cocoa 中实现这一点的正确语法是什么?
Thanks so much!
非常感谢!
回答by Vladimir
To create an array:
创建数组:
NSMutableArray* africa = [NSMutableArray arrayWithObjects: @"Lion", @"Tiger", @"Zebra", nil];
NSMutableArray* canada = [NSMutableArray arrayWithObjects: @"Polar bear", @"Beaver", @"Loon", nil];
To combine two arrays you can initialize array with elements of the 1st array and then add elements from 2nd to it:
要组合两个数组,您可以使用第一个数组的元素初始化数组,然后将第二个数组的元素添加到它:
NSMutableArray* animals = [NSMutableArray arrayWithArray:africa];
[animals addObjectsFromArray: canada];
回答by Minthos
Based on Vladimir's answer I wrote a simple function:
根据弗拉基米尔的回答,我写了一个简单的函数:
NSMutableArray* arrayCat(NSArray *a, NSArray *b)
{
NSMutableArray *ret = [NSMutableArray arrayWithCapacity:[a count] + [b count]];
[ret addObjectsFromArray:a];
[ret addObjectsFromArray:b];
return ret;
}
but I haven't tried to find out if this approach is faster or slower than Vladimir's
但我没有试图找出这种方法是否比 Vladimir 的更快或更慢