iphone - Data not save in NSUserDefaults -


i having problem nsuserdefaults. when type in name in textbox , click on highscore application not response , keypad still on screen. when tried load data, data name nil. score have 90. can please tell me wrong coding.

lots of thanks.

-(ibaction)savehighscore_button {       int i, ii = -1;      struct high_score {         nsstring *name;         int highscore;     };      struct high_score structarray[10];      nsuserdefaults *userpreferences = [nsuserdefaults standarduserdefaults];      (i=0; i<10; i++) {          if ([userpreferences stringforkey :[nsstring stringwithformat:@"highscorenameentry%d",i]]!=nil && [userpreferences stringforkey :[nsstring stringwithformat:@"highscoreentry%d"]]!=nil) {         structarray[i].name= [userpreferences stringforkey:[nsstring stringwithformat:@"highscorenameentry%d",i]];             structarray[i].highscore = [userpreferences integerforkey:[nsstring stringwithformat:@"highscoreentry%d",i]];             ii = i;         }     }     if (myscore >0) {         (i==ii; i>=0; i--) {             if (myscore > structarray[i].highscore) {                 if (i<9) {                     structarray[i+1] = structarray[i];                     structarray[i].name = nametextbox.text;                     structarray[i].highscore = myscore;                      if (i==ii && i<9) {                         structarray[i+1].name = nametextbox.text;                         structarray[i+1].highscore = myscore;                         ii=i+i;                     }                      else if(i==ii && i<9) {                         structarray[i+1].name = nametextbox.text;                         structarray[i+1].highscore = myscore;                         ii=i+1;                     }                 }             }              if (ii==-1 && myscore >0) {                 structarray[0].name = nametextbox.text;                 structarray[0].highscore = myscore;                 ii=0;             }             (i=0; i<=ii; i++) {                 [userpreferences setobject:structarray[i].name forkey:[nsstring stringwithformat:@"highscorenameentry%d",i]];                 [userpreferences setinteger:structarray[i].highscore forkey:[nsstring stringwithformat:@"highscoreentry%d",i]];             }         }     }  } 

anything saved user defaults must of standard plist-compliant class (nsarray, nsdictionary, nsnumber, nsstring, nsdate, nsdata, nsvalue). if it's not 1 of those, has archived nsdata instance (and therefore nscoding compliant) or packed nsvalue if it's compatible.

that said, you're making far more difficult on need to. why not have array (the high score descriptor container) of dictionaries (each descriptor high score) hold name (an nsstring) , score (an nsnumber)? each dictionary describes high score given name. can store whole thing using -setobject:yourarray forkey:@"highscores".

since immutable copies nsuserdefaults, you'll want ask -mutablecopy of high scores array (don't forget release it) when want modify things. either replace score or delete/add.

using approach, don't have resort (sorry this) ghastly "string#" approach fixed number of scores. array contain existing high scores. no waste , 100% standard cocoa classes plist'able no work. lets sort array using sort descriptors (sorted key used store score number in dictionary).

some basic code:

/* build fake high scores array */  nsmutablearray * highscores = [nsmutablearray array];  // bob's score nsdictionary * bob = [nsdictionary dictionarywithobjectsandkeys:         @"bob", @"name",          [nsnumber numberwithint:8234323], @"score",          nil]; [highscores addobject:bob];  // jane's score nsdictionary * jane = [nsdictionary dictionarywithobjectsandkeys:         @"jane", @"name",          [nsnumber numberwithint:92346345], @"score",          nil]; [highscores addobject:jane];  // donald's score nsdictionary * donald = [nsdictionary dictionarywithobjectsandkeys:         @"donald", @"name",          [nsnumber numberwithint:5272348], @"score",          nil]; [highscores addobject:donald];  // sort high score nssortdescriptor * sort = [[nssortdescriptor alloc] initwithkey:@"score"                                                        ascending:no]; [highscores sortusingdescriptors:[nsarray arraywithobject:sort]]; [sort release];  // store in user defaults [[nsuserdefaults standarduserdefaults] setobject:highscores                                            forkey:@"highscores"]; 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -