nsuserdefaults - iOS Terminating app due to uncaught exception 'NSUnknownKeyException' -


i pretty new objective c , ios programming, , have pretty strange error. app in question initializes nsmutablearray preset set of values of custom type made using nsobject. manipulated app. if new values added during app run time, saved using nsuserdefaults, , brought nsuserdefaults along default values on next app open.

this error get:

terminating app due uncaught exception 'nsunknownkeyexception', reason: '[<__nscfconstantstring 0xb404> valueforundefinedkey:]: class not key value coding-compliant key score.' *** first throw call stack: (0x1c9b012 0x10d8e7e 0x1d23fb1 0xb84d1d 0xaf100b 0xaf0fbd 0xb0f247 0xb3023c 0xb30056 0x3e40 0x3c5f 0x11f5ad 0x10ec705 0x202c0 0x20258 0x242ff4 0x10ec705 0x202c0 0x20258 0xe1021 0xe157f 0xe1056 0x246af9 0x10ec705 0x202c0 0x20258 0xe1021 0xe157f 0xe06e8 0x4fcef 0x4ff02 0x2dd4a 0x1f698 0x1bf6df9 0x1bf6ad0 0x1c10bf5 0x1c10962 0x1c41bb6 0x1c40f44 0x1c40e1b 0x1bf57e3 0x1bf5668 0x1cffc 0x290d 0x2835) libc++abi.dylib: terminate called throwing exception 

i not quite sure error or how go debugging it.

previously code worked flawlessly, did remove 1 or 2 elements preset default list of elements, , in simulator, simulated deleting app, , recompiled code. ever since program crashes, above message, , can't figure out how fix it.

so if can give me on how go debugging this, wonderful. can attach code needed, i'm not sure code relevant shown, , may post code involved in project.

code encode , decode properties of custom name nsobject class called name.h:

-(void)encodewithcoder:(nscoder *)encoder {     [encoder encodeobject:self.name forkey:@"name"];     [encoder encodeinteger:self.score forkey:@"score"];  }  -(id)initwithcoder:(nscoder *)decoder {     if((self = [super init]))     {         self.name = [decoder decodeobjectforkey:@"name"];         self.score = [decoder decodeintegerforkey:@"score"];     }      return self; } 

retrieving data class, incase matters, code occurs in appdelegate.m:

nsdata *data = [[nsuserdefaults standarduserdefaults] objectforkey:@"dataarray"];   nsinteger score = 0;   nsmutablearray *temp = [[nsmutablearray alloc] initwithobjects:@"name", nil];  nsmutablearray *templist = [[nsmutablearray alloc] init];  for(nsstring *y in temp) {     name *name = [[name alloc] init];     name.name  = y;     name.score = score;     [templist addobject:name];   }  if (data) {      nsarray *list = [nskeyedunarchiver unarchiveobjectwithdata:data];    nsmutablearray *names = [[nsmutablearray alloc]initwitharray:list];   // [_namelist addobjectsfromarray:temp];     nsmutablearray *t = [[names arraybyaddingobjectsfromarray:templist ] mutablecopy];      _namelist = [[nsmutablearray alloc]init];      [_namelist addobjectsfromarray:t];   } else  {     //first time load or data not saved yet     _namelist = [[nsmutablearray alloc] initwithobjects:@"name", nil];  } 

saving array @ close time:

- (void)applicationwillterminate:(uiapplication *)application { // called when application terminate. save data if appropriate. see applicationdidenterbackground:. nsdata *data =[nskeyedarchiver archiveddatawithrootobject:_namelist];   [[nsuserdefaults standarduserdefaults] setobject:data forkey:@"dataarray"]; } 

same code in applicationdidenterbackground.

code sort 'score'

- (void)tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didselectviewcontroller:    (uiviewcontroller *)viewcontroller {       if(viewcontroller == _viewcontroller3)     {         [self sortnames:_namelist];         [[(thirdviewcontroller*)_viewcontroller3 toplist] reloaddata];      } }  -(void)sortnames:(nsmutablearray*)test {    nsarray* temp = [[nsarray alloc] initwitharray:test];   nssortdescriptor *sortdescriptor = [nssortdescriptor sortdescriptorwithkey:@"score" ascending:no]; nsarray *sortedlinks = [[temp sortedarrayusingdescriptors:[nsarray arraywithobject:sortdescriptor]] mutablecopy];  _namelist = (nsmutablearray*) sortedlinks; } 

here's 2 cents. have line:

[encoder encodeobject:self.name forkey:@"name"]; 

and line:

name *name = [[name alloc] init]; 

this makes me think "self.name" property 1 of these "name" custom subclasses.

i believe if make custom subclass , want encodewithcoder, have add encodewithcoder method custom subclass , have encodewithcoder of properties , instance variables primitively can.

meaning, name class needs have own encodewithcoder method encodes of properties , instance variables have been stored factory objects or c primitives.

edit: i'm still pretty new , value rep. if i'm wrong, please comment , i'll delete please don't downvote me oblivion


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -