ios - Mutable Objects inside NSUserDefaults -


i created simple database , put in nsuserdefaults. database nsmutablearray has dictionaries , arrays inside in it. when create nsmutablearray nsuserdefaults can't add objects mutable objects inside nsmutablearray. here code:

nsmutablearray *arrayone = [nsmutablearray arraywithcontentsoffile:[self createeditablecopyofifneededwithfilename:@"form.plist"]];  nsuserdefaults *ayarlar = [nsuserdefaults standarduserdefaults];  [ayarlar setobject:arrayone forkey:@"form"];  nsmutablearray *arraytwo = [nsmutablearray arraywitharray:[[ayarlar objectforkey:@"form"] mutablecopy]];  [[[arraytwo objectatindex:0] objectforkey:@"itemlar"] addobject:@"hop"]; 

and here error:

'nsinternalinconsistencyexception', reason: '-[__nscfarray insertobject:atindex:]: mutating method sent immutable object'

how can make work? thank everyone.

nsuserdefaults not right place store data. first of should used small amounts of data, such settings. , secondly returns immutable objects, if set mutable ones. making mutable copy of first array doesn’t because array mutable. inside array isn’t touched mutablecopy method , stay immutable.

you should use nspropertylistserialization class read , write data file. on reading can pass options controlling mutability of read objects. there want pass nspropertylistmutablecontainers or nspropertylistmutablecontainersandleaves.

with first containers (arrays , dictionaries is) mutable. latter leaves (that nsstring , nsdata objects) mutable.

depending on how big data set can should use real database or core data instead.


Comments