當我嘗試將對象添加到檢索到的NSMutableArray
時,我不斷收到此錯誤。將新對象添加到檢索的NSMutableArray - 返回NSInternalInconsistencyException
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
檢索:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *arrayOfTitles = [userDefaults objectForKey:@"mainArraySaveData"];
NSMutableArray *arrayOfSubjects = [userDefaults objectForKey:@"subjectArraySaveData"];
NSMutableArray *arrayOfDates = [userDefaults objectForKey:@"dateArraySaveData"];
_mutableArray=arrayOfTitles;
_subjectArray=arrayOfSubjects;
_dateArray=arrayOfDates;
[_tableView reloadData];
節省:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
_mutableArray = [[NSMutableArray alloc] initWithArray:[userDefaults objectForKey:@"mainArraySaveData"]];
[userDefaults setObject:_mutableArray forKey:@"mainArraySaveData"];
[userDefaults synchronize];
_dateArray = [[NSMutableArray alloc] initWithArray: [userDefaults objectForKey:@"dateArraySaveData"]];
[userDefaults setObject:_dateArray forKey:@"dateArraySaveData"];
[userDefaults synchronize];
_subjectArray = [[NSMutableArray alloc] initWithArray: [userDefaults objectForKey:@"subjectArraySaveData"]];
[userDefaults setObject:_subjectArray forKey:@"subjectArraySaveData"];
[userDefaults synchronize];
我很困惑,我認爲這是旨在返回NSMutableArray
,但它說沒有 - NSArray
。我的問題是什麼? 謝謝,SebOH
謝謝你洙多! –
第一行結束了幫助我的問題,_mutableArray = [arrayOfTitles mutableCopy]; –