xcode NSMutableArray 的 NSMutableArray。发送到不可变对象的变异方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14840437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:45:38  来源:igfitidea点击:

NSMutableArray of NSMutableArrays. Mutating method sent to immutable object

iphoneiosobjective-cxcodecocoa

提问by SmartTree

I have NSMutableArrayof NSMutableArrays:

NSMutableArrayNSMutableArrays

 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];
        }

Then, when I try to do this:

然后,当我尝试这样做时:

 [[array objectAtIndex:packIndex]replaceObjectAtIndex:index withObject:@"1"];

it crashes with: [__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'

它崩溃了: [__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'

Why ? How to fix ? Thanks !

为什么 ?怎么修 ?谢谢 !

UPD: I save this array in NSUserDefaults:

UPD:我将此数组保存在NSUserDefaults

[defaults setObject:array forKey:@"mainArray"];

Then, I read it in the other class:

然后,我在另一堂课上读到:

array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"mainArray"]];

Also, I must to mention that sometimes the code runs well and it changes "0" to "1". But it also crashes sometimes. So I cant see the logic, why it works fine or why it crashes sometimes.

另外,我必须提到,有时代码运行良好,它会将“0”更改为“1”。但它有时也会崩溃。所以我看不到逻辑,为什么它工作正常或为什么有时会崩溃。

回答by Lance

The problem is that when you read the array out of NSUserDefaults, the mini-arrays are not automatically NSMutableArrays.

问题是当您从 NSUserDefaults 中读取数组时,小数组不会自动成为 NSMutableArrays。

Try this:

尝试这个:

array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"mainArray"]];
for(int i = 0; i < array.count; i++) {
    NSArray * tempArray = array[i];
    array[i] = [tempArray mutableCopy];
}

Edit: Best Coder's answer explains why this is. Objects stored in NSUserDefaults are stored as immutable versions, basically NSUserDefaults is a plist and there is no flag marking an array as mutable/immutable so when you read them back out, they are assumed to be immutable.

编辑:Best Coder 的回答解释了为什么会这样。存储在 NSUserDefaults 中的对象存储为不可变版本,基本上 NSUserDefaults 是一个 plist 并且没有将数组标记为可变/不可变的标志,因此当您将它们读回时,它们被假定为不可变的。

回答by aks.knit1108

Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for "MyStringDefault", the string you later retrieve using stringForKey: will be immutable.

从 NSUserDefaults 返回的值是不可变的,即使您将可变对象设置为值。例如,如果您将可变字符串设置为“MyStringDefault”的值,那么您稍后使用 stringForKey: 检索的字符串将是不可变的。

Instead, make a mutableCopy of the array you retrieve from NSUserDefaults, add your object, then set your new array back in.

相反,制作从 NSUserDefaults 检索到的数组的 mutableCopy,添加对象,然后重新设置新数组。

see this link: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

请参阅此链接:http: //developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

回答by Chris C

Try using more verbose code:

尝试使用更详细的代码:

NSMutableArray *tempArray = [array objectAtIndex:packIndex];
[tempArray replaceObjectAtIndex:index withObject:@"1"];