iphone - Nsuserdefauls problem -
i have typed coding under highscore button. suppose string text(name) in textbox , save highscore. however, not find plist being created under document. please help
-(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]]; } } } }
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *writableplistpath = [documentsdirectory stringbyappendingpathcomponent:@"my.plist"]; if(![[nsfilemanager defaultmanager]fileexistsatpath:writableplistpath]){ bool success; nsstring *defaultplistpath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"my.plist"]; success = [[nsfilemanager defaultmanager] copyitematpath:defaultplistpath topath:writableplistpath error:nil]; //for getting content of plist plistarray = [nsarray arraywithcontentsoffile:writableplistpath]; }
this code used saving plist in document directory first time(at launch app first time on simulator or device) save plist in document directory can access data of plist.by same code change occur in if condition ! opertor should not there , plistarray have plist data can access via array.
Comments
Post a Comment