我NSMutableArray
有NSMutableArrays
:
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i = 0; i < 5; i++)
{
NSMutableArray *miniArray = [[NSMutableArray alloc]init];
for (int k = 0; k < 30; k++)
{
[miniArray addObject:@"0"];
}
[array addObject:miniArray];
}
然后,当我尝试这样做时:
[[array objectAtIndex:packIndex]replaceObjectAtIndex:index withObject:@"1"];
它崩溃了:[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
为什么 ?怎么修 ?谢谢 !
UPD:我将此数组保存在NSUserDefaults
:
[defaults setObject:array forKey:@"mainArray"];
然后,我在另一堂课上读到它:
array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"mainArray"]];
另外,我必须提到,有时代码运行良好,它会将“0”更改为“1”。但它有时也会崩溃。所以我看不到逻辑,为什么它工作正常或为什么有时会崩溃。