objective c - Xcode - Most Streamlined way of saving a lot of integers to a file -
i saving 30 integers file creating nsmutabledictionary , using nskeyedarchiver save it.
@interface: nsinteger highscoree; nsinteger highscorem; nsinteger highscoreh; nsinteger highscorei; nsinteger highscorecome; nsinteger highscorecomm; nsinteger highscorecomh; nsinteger highscorecomi; nsinteger totalgameswone; nsinteger totalgameswonm; nsinteger totalgameswonh; nsinteger totalgameswoni; nsinteger totalgamesloste; nsinteger totalgameslostm; nsinteger totalgameslosth; nsinteger totalgameslosti; nsinteger totalpointsfore; nsinteger totalpointsform; nsinteger totalpointsforh; nsinteger totalpointsfori; nsinteger totalpointsagainste; nsinteger totalpointsagainstm; nsinteger totalpointsagainsth; nsinteger totalpointsagainsti; nsinteger highscore; nsinteger highscorecom; nsinteger totalgames; nsinteger totalgameswon; nsinteger totalgameslost; nsinteger totalpointsfor; nsinteger totalpointsagainst; @property(nonatomic) nsinteger highscoree; @property(nonatomic) nsinteger highscorem; @property(nonatomic) nsinteger highscoreh; @property(nonatomic) nsinteger highscorei; @property(nonatomic) nsinteger highscorecome; @property(nonatomic) nsinteger highscorecomm; @property(nonatomic) nsinteger highscorecomh; @property(nonatomic) nsinteger highscorecomi; @property(nonatomic) nsinteger totalgameswone; @property(nonatomic) nsinteger totalgameswonm; @property(nonatomic) nsinteger totalgameswonh; @property(nonatomic) nsinteger totalgameswoni; @property(nonatomic) nsinteger totalgamesloste; @property(nonatomic) nsinteger totalgameslostm; @property(nonatomic) nsinteger totalgameslosth; @property(nonatomic) nsinteger totalgameslosti; @property(nonatomic) nsinteger totalpointsfore; @property(nonatomic) nsinteger totalpointsform; @property(nonatomic) nsinteger totalpointsforh; @property(nonatomic) nsinteger totalpointsfori; @property(nonatomic) nsinteger totalpointsagainste; @property(nonatomic) nsinteger totalpointsagainstm; @property(nonatomic) nsinteger totalpointsagainsth; @property(nonatomic) nsinteger totalpointsagainsti; @property(nonatomic) nsinteger highscore; @property(nonatomic) nsinteger highscorecom; @property(nonatomic) nsinteger totalgames; @property(nonatomic) nsinteger totalgameswon; @property(nonatomic) nsinteger totalgameslost; @property(nonatomic) nsinteger totalpointsfor; @property(nonatomic) nsinteger totalpointsagainst; @implementation; nsstring *nsstotalpointsfor = [nsstring stringwithformat:@"%i", totalpointsfor]; nsstring *nsstotalpointsfore = [nsstring stringwithformat:@"%i", totalpointsfore]; nsstring *nsstotalpointsform = [nsstring stringwithformat:@"%i", totalpointsform]; nsstring *nsstotalpointsforh = [nsstring stringwithformat:@"%i", totalpointsforh]; nsstring *nsstotalpointsfori = [nsstring stringwithformat:@"%i", totalpointsfori]; //create dictionary nsmutabledictionary* mydict = [[nsmutabledictionary alloc] init]; //add things dictionary (game stats) [mydict setobject:nsstotalpointsfor forkey:@"totalpointsfor"]; [mydict setobject:nsstotalpointsfore forkey:@"totalpointsfore"]; [mydict setobject:nsstotalpointsform forkey:@"totalpointsform"]; [mydict setobject:nsstotalpointsforh forkey:@"totalpointsforh"]; [mydict setobject:nsstotalpointsfori forkey:@"totalpointsfori"]; //get path nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentpath = [paths objectatindex:0]; nsstring *path = [documentpath stringbyappendingpathcomponent:@"stats.save"]; // save file [nskeyedarchiver archiverootobject:mydict tofile:path]; and other integers.
then have read these strings integers:
//get path nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentpath = [paths objectatindex:0]; nsstring *path = [documentpath stringbyappendingpathcomponent:@"stats.save"]; //create dictionary nsmutabledictionary* mydict = [[nsmutabledictionary alloc] init]; //read file mydict = [nskeyedunarchiver unarchiveobjectwithfile:path]; nsstring *nssplayerhighscore = [mydict objectforkey:@"playerhighscore"]; highscore = [nssplayerhighscore intvalue]; nsstring *nsscomputerhighscore = [mydict objectforkey:@"computerhighscore"]; highscorecom = [nsscomputerhighscore intvalue]; nsstring *nsstotalpointsfor = [mydict objectforkey:@"totalpointsfor"]; totalpointsfor = [nsstotalpointsfor intvalue]; nsstring *nsstotalpointsagainst = [mydict objectforkey:@"totalpointsagainst"]; totalpointsagainst = [nsstotalpointsagainst intvalue]; nsstring *nsstotalgames = [mydict objectforkey:@"totalgames"]; totalgames = [nsstotalgames intvalue]; nsstring *nsstotalgameswon = [mydict objectforkey:@"totalgameswon"]; totalgameswon = [nsstotalgameswon intvalue]; nsstring *nsstotalgameslost = [mydict objectforkey:@"totalgameslost"]; totalgameslost = [nsstotalgameslost intvalue]; nsstring *nssplayerhighscoree = [mydict objectforkey:@"playerhighscoree"]; highscoree = [nssplayerhighscoree intvalue]; nsstring *nsscomputerhighscoree = [mydict objectforkey:@"computerhighscoree"]; highscorecome = [nsscomputerhighscoree intvalue]; and on other integers well.
i find way extremely cumbersome , annoying don't know other ways save integers.
what streamlined way of saving things in general?
i don't know if nskeyedarchiver problem or should making strings contain more 1 integer (eg.nsstring *nsstotalpointsfor = [nsstring stringwithformat:@"%i, %i, ...", totalpointsfor, totalpointsagainst, ...];.
but how change numbers in string integers?
or use array somehow?
give me idea , open many suggestions.
thx lot don't know programing.
storing these numbers "nsstring" objects , archiving them big block of code indeed seem cumbersome.
why not "nsnumber" objects, have archiving capability built in? throw "nsnumber" objects array (e.g. "highscoreenumber = [[nsnumber alloc] initwithint: highscoree];") , write file , load file on subsequent launches.
edit:
i can't port of code you, check out:
@interface: { nsnumber * highscore; nsnumber * highscorecom; } @property(nonatomic, retain) nsnumber * highscore; @property(nonatomic, retain) nsnumber * highscorecom; @implementation; //create dictionary nsmutabledictionary* mydict = [[nsmutabledictionary alloc] init]; if(mydict) { //add things dictionary (game stats) [mydict setobject:highscore forkey:@"highscore"]; [mydict setobject:highscorecom forkey:@"highscorecom"]; //get path nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentpath = [paths objectatindex:0]; nsstring *path = [documentpath stringbyappendingpathcomponent:@"stats.save"]; bool successfulwrite = [mydict writetofile: path atomically: yes]; if(successfulwrite == no) { nslog( @"could not write file path %@", path ); } // if *not* using arc, don't forget release [mydict release]; }
Comments
Post a Comment