iPhone SDK:如何将NSArray的内容复制到NSMutable数组? [英] iPhone SDK: How to copy the contents of a NSArray to NSMutable array?

查看:65
本文介绍了iPhone SDK:如何将NSArray的内容复制到NSMutable数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆