iphone - Storing NSMutableArray filled with custom objects in NSUserDefaults - crash -
edit: here working version. able retrieve object within nsmutablearray after saved , loaded nsuserdefaults via nscoding. think it's important mention, not need de-archive array, content. can see, had not store nsdata of freeze object, nsdata of array:
// class "freeze" @interface freeze : nsobject <nscoding> // nscoding-protocoll important!! { nsmutablestring *name; } @property(nonatomic, copy) nsmutablestring *name; -(void) initstring; @end @implementation freeze @synthesize name; -(void) initstring { name = [[nsmutablestring stringwithstring:@"some sentence... lalala"] retain]; } // method nscoding-protocol - (void)encodewithcoder:(nscoder *)encoder { //encode properties, other class variables, etc [encoder encodeobject:self.name forkey:@"name"]; } // method nscoding-protocol - (id)initwithcoder:(nscoder *)decoder { self = [super init]; if( self != nil ) { //decode properties, other class vars self.name = [decoder decodeobjectforkey:@"name"]; } return self; } @end freeze *freeze; nsmutablearray *runes; nsmutablearray *newrunes; runes = [[nsmutablearray alloc] init]; newrunes = [[nsmutablearray alloc] init]; freeze = [[freeze alloc] init]; [freeze initstring]; [runes addobject:freeze]; [self savestate]; [self restorestate]; freeze *newfreeze = [[freeze alloc] init]; newfreeze = [newrunes objectatindex:0]; nsstring *string = [nsstring stringwithstring:newfreeze.name]; nslog(@"%@", string); //----------------------------------------------------------------------------- - (void) savestate { nsuserdefaults* defaults = [nsuserdefaults standarduserdefaults]; nsdata* myclassdata = [nskeyedarchiver archiveddatawithrootobject:freeze]; [defaults setobject:myclassdata forkey:@"myclass"]; nsdata* myclassarraydata = [nskeyedarchiver archiveddatawithrootobject:runes]; [defaults setobject:myclassarraydata forkey:@"myclassarray"]; } - (void) restorestate { nsuserdefaults* defaults = [nsuserdefaults standarduserdefaults]; nsdata* myclassdata = [defaults objectforkey:@"myclass"]; freeze = [nskeyedunarchiver unarchiveobjectwithdata:myclassdata]; nsdata* myclassarraydata = [defaults objectforkey:@"myclassarray"]; nsarray *savedmyclassarray = [nskeyedunarchiver unarchiveobjectwithdata:myclassarraydata]; if( savedmyclassarray != nil ) newrunes = [[nsmutablearray alloc] initwitharray:savedmyclassarray]; else newrunes = [[nsmutablearray alloc] init]; }
edit: error got before, doesn't show anymore updated version above.
it crashes @ end , debugger reveals following error: ** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[nsconcretemutabledata initstring]: unrecognized selector sent instance 0x6b1fe20'*
furthermore, says "newfreeze" not of type cfstring. have clue what's going on? want save objects that.
the problem storing custom classes node in settings plist (nsuserdefaults) not allowed, since data stored in file system rather object application. settings app (where data visible) has no idea "freeze" object is. best way handle want use core data.
also of note: error getting caused @ end of method when try initalize new freeze
object runes
array, because when put object runes array encapsulated nsdata
object first, when out don't dearchive before setting newfreeze
Comments
Post a Comment