![](/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.