![](/img/trans.png)
[英]How to copy NSMutableArray value into another NSMutableArray in iPhone?
[英]How to copy the contents of NSMutableArray to another NStArray in iphone app?
我有两个数组一个是NSMutableArray,一个是NSArray我想在NSArray中存储NSMutableArray的内容,但是它不适用于我发送异常无法识别的选择器。
myArray=[[NSArray alloc] initWithArray:appDelegate.surveyAnswersScreenOne];
注意, SurveyAnswerScreenOne
是一个NSMutableArray
你可以用很多方式做到这一点 -
NSArray * myArray = [appDelegate.surveyAnswersScreenOne copy];
NSArray * myArray = [NSArray arrayWithArray:appDelegate.surveyAnswersScreenOne];
NSArray * myArray = [[NSArray alloc]initWithArray:appDelegate.surveyAnswersScreenOne];
但首先你的appDelegate.surveyAnswersScreenOne
应该包含对象。
从我们在这里看到的,你的可变数组很可能是零。 在你的app delegate中查看它的创建。 如果创建正确,请检查是否保留。 这是一个强有力的参考?
@propery(nonatomic, strong) NSMutableArray *surveyAnswersScreenOne;
一,我会使用方便的方法:
myArray = [NSArray arrayWithArray:appDelegate.surveyAnswersScreenOne];
如果surveyAnswersScreenOne是一个有效的数组,可变或其他,这应该工作。 尝试将其打印到控制台以确保。 如果surveyAnswersScreenOne为nil,则返回空数组,其中alloc initWithArray将失败。 检查你这样的可变数组。
NSLog(@"Mutable array is %@", appDelegate.surveyAnswersScreenOne);
你有没有为appDelegate
制作对象?
appDelegate = (yourDelegateClass *)[[UIApplication sharedApplication]delegate];
如果是,那么其他答案应该有效!
myArray = [NSArray arrayWithArray:appDelegate.surveyAnswersScreenOne];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.