Getting “mutating method sent to immutable object” error

后端 未结 3 1059
忘掉有多难
忘掉有多难 2020-12-12 03:06

I can\'t figure out what is causing this. Basically, a few different \'tasks\' are colliding with each other in my app. When i press a button, it runs this code just fine:

3条回答
  •  旧时难觅i
    2020-12-12 03:27

    NSUserDefaults always returns immutable objects, even if what you stored was mutable. To work around this, you need to make a mutable copy. Since -mutableCopy returns an object that the caller owns, it needs to be (auto)released:

    dataCenter.colourPalettesContainer = [[[prefs objectForKey:@"container"] mutableCopy] autorelease];
    

    (Edit) I posted some -mutableDeepCopy NSArray & NSDictionary methods a while back, in response to another question. If your problem involves deeper nesting of collections, and you need them all to be mutable, this may help.

提交回复
热议问题