简体   繁体   English

如何将NSMutableArray的内容复制到iphone应用程序中的另一个NStArray?

[英]How to copy the contents of NSMutableArray to another NStArray in iphone app?

I have two array one is NSMutableArray and one is NSArray i want to store the contents of NSMutableArray in NSArray but it is not working for me gives exception unrecognised selector sent. 我有两个数组一个是NSMutableArray,一个是NSArray我想在NSArray中存储NSMutableArray的内容,但是它不适用于我发送异常无法识别的选择器。

myArray=[[NSArray alloc] initWithArray:appDelegate.surveyAnswersScreenOne];

Note, SurveyAnswerScreenOne is an NSMutableArray 注意, SurveyAnswerScreenOne是一个NSMutableArray

You can do that in many ways - 你可以用很多方式做到这一点 -

NSArray * myArray = [appDelegate.surveyAnswersScreenOne copy];

NSArray * myArray = [NSArray arrayWithArray:appDelegate.surveyAnswersScreenOne];

NSArray * myArray = [[NSArray alloc]initWithArray:appDelegate.surveyAnswersScreenOne];

But first of all your appDelegate.surveyAnswersScreenOne should have objects in it. 但首先你的appDelegate.surveyAnswersScreenOne应该包含对象。

From what we see here, it is most likely that your mutable array is nil. 从我们在这里看到的,你的可变数组很可能是零。 Look into the creation of that in you app delegate. 在你的app delegate中查看它的创建。 If it is created properly, check that it is retained. 如果创建正确,请检查是否保留。 Is it a strong reference? 这是一个强有力的参考?

@propery(nonatomic, strong) NSMutableArray *surveyAnswersScreenOne;

for one, I would use the convenience method: 一,我会使用方便的方法:

myArray = [NSArray arrayWithArray:appDelegate.surveyAnswersScreenOne];

If surveyAnswersScreenOne is a valid array, mutable or otherwise, this should work. 如果surveyAnswersScreenOne是一个有效的数组,可变或其他,这应该工作。 Try printing it to the console to be sure. 尝试将其打印到控制台以确保。 This will return empty array if surveyAnswersScreenOne is nil, where alloc initWithArray will fail. 如果surveyAnswersScreenOne为nil,则返回空数组,其中alloc initWithArray将失败。 Check you mutable array like this. 检查你这样的可变数组。

NSLog(@"Mutable array is %@", appDelegate.surveyAnswersScreenOne);

Have you made object for your appDelegate ? 你有没有为appDelegate制作对象?

appDelegate = (yourDelegateClass *)[[UIApplication sharedApplication]delegate];

If yes, then other answers should work! 如果是,那么其他答案应该有效!

myArray = [NSArray arrayWithArray:appDelegate.surveyAnswersScreenOne];

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

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