Reputation: 57
i am currently saving 30 integers to a file by creating a NSMutableDictionary and then using the NSKeyedArchiver to 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 to 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 the path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingPathComponent:@"stats.save"];
// save to file
[NSKeyedArchiver archiveRootObject:myDict toFile:path];
and then i do this similarly for the other integers.
Then I have to read these strings back into the integers:
//get the 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 from 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 then so on for the other integers as well.
I find this way extremely cumbersome and annoying but I don't know any other ways to save integers.
What is the most streamlined way of saving things in general?
I don't know if NSKeyedArchiver is the problem or should I be making the strings contain more than one integer (eg.NSString *nssTotalPointsFor = [NSString stringWithFormat:@"%i, %i, ...", totalPointsFor, totalPointsAgainst, ...];.
But then how would I change the numbers in the string back into the integers?
Or do I use an array somehow?
Give me your I idea and I am open to many suggestions.
Thx a lot I don't know much about programing.
Upvotes: 2
Views: 600
Reputation: 89509
Storing these numbers into "NSString
" objects and archiving them with that big block of code does indeed seem really cumbersome.
Why not do this with "NSNumber
" objects, which have archiving capability built in? You could throw all those "NSNumber
" objects into an array (e.g. "highScoreENumber = [[NSNumber alloc] initWithInt: highScoreE];
") and write it to a file and then load up the file on subsequent launches.
EDIT:
I can't port of your code for you, but check this 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 to dictionary (game stats)
[myDict setObject:highScore forKey:@"highScore"];
[myDict setObject:highScoreCom forKey:@"highScoreCom"];
//get the 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 my file to path %@", path );
}
// if *not* using ARC, don't forget to release
[myDict release];
}
Upvotes: 3