insert object in an NSMutableArray saved with NSUserDefaults

NSUserDefaults always returns immutable objects, even if the original object was mutable. It’s in the documentation for objectForKey:

The returned object is immutable, even if the value you originally set was mutable.

You will need to create a copy of the returned object before you modify it, using [NSMutableArray arrayWithArray:]

Probably also best to use the arrayForKey method of NSUserDefaults if you’re retrieving an array. Docs here: https://developer.apple.com/documentation/foundation/userdefaults

Leave a Comment