2

我目前通过创建 NSMutableDictionary 然后使用 NSKeyedArchiver 将 30 个整数保存到文件中。

@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];

然后我对其他整数进行类似的操作。

然后我必须将这些字符串读回整数:

//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];

然后其他整数也是如此。

我发现这种方式非常麻烦和烦人,但我不知道还有其他保存整数的方法。

一般来说,最精简的保存方式是什么?

我不知道 NSKeyedArchiver 是否有问题,或者我是否应该让字符串包含多个整数(例如 NSString *nssTotalPointsFor = [NSString stringWithFormat:@"%i, %i, ...", totalPointsFor, totalPointsAgainst, ...];.

但是,我如何将字符串中的数字改回整数呢?

还是我以某种方式使用数组?

给我你的想法,我愿意接受很多建议。

谢谢很多我对编程不太了解。

4

1 回答 1

3

将这些数字存储到“ NSString”对象中并用那一大块代码归档它们确实看起来很麻烦。

为什么不使用内置归档功能的NSNumber”对象呢?您可以将所有这些“ NSNumber”对象放入一个数组(例如“ highScoreENumber = [[NSNumber alloc] initWithInt: highScoreE];”)并将其写入文件,然后在后续启动时加载该文件。

编辑:

我无法为您移植您的代码,但请检查一下:

@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];
}
于 2012-06-05T10:01:50.613 回答