iPhone SDK:如何将NSArray的内容复制到NSMutable数组? [英] iPhone SDK: How to copy the contents of a NSArray to NSMutable array?
问题描述
我需要将NSArray的内容复制到NSMutable数组.换句话说,我想将arrayCountryChoices复制到arraySearchResults.有什么想法吗??
I need to copy the contents of an NSArray to NSMutable array. In other words, I want to copy arrayCountryChoices to arraySearchResults. Any ideas????
//main data
NSArray *arrayCountryChoices;
//search results buffer
NSMutableArray *arraySearchResults;
//create data
arrayCountryChoices = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil];
//copy the original array to searchable array ->> THIS IS NOT WORKING AS EXPECTED
arraySearchResults = [[NSMutableArray alloc] arrayWithArray:arrayCountryChoices];
谢谢.
推荐答案
是
[NSMutableArray arrayWithArray:anArray];
或
[[NSMutableArray alloc] initWithArray:anArray];
或
[anArray mutableCopy];
示例中的代码不起作用,因为您正在NSMutableArray
的实例上调用arrayWithArray
,但是arrayWithArray
是一个类方法.
The code in your example doesn't work because you're calling arrayWithArray
on an instance of NSMutableArray
, but arrayWithArray
is a class method.
通常,以init
开头的初始化方法是实例方法,而以类名(array
等)开头的初始化方法是类方法.类方法返回自动释放的对象,而实例方法返回保留的对象.
As a general rule, initalization methods that start with init
are instance methods, and those that start with the name of the class (array
, etc.) are class methods. Class methods return autoreleased objects, while instance methods return retained objects.
这篇关于iPhone SDK:如何将NSArray的内容复制到NSMutable数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!