Advice on High Score persistence (iPhone, Cocoa Touch)

核能气质少年 提交于 2019-12-04 16:33:48

Both your ways look fine to me. There is also Core Data in the 3.0 OS, although it might be overkill if all you want to save is a single high score value.

I'm not sure that I understand your objections! Both should work just fine.

Personally I prefer method A. I think it makes sense that an object knows how to encode itself. It makes maintenance easier and any changes more localised. Plus it probably uses less memory.

Konstantinos

check out this question, maybe it's useful for your app

I went with method B because: 1. The plist file is more readable and 2. I can save off some file and class version numbering into this method easily:

In my HighScore class:

- (id)initFromDictionary: (NSDictionary *)dict;
{
    if (self = [super init]) {
        self.name = [dict objectForKey:kHighScoreNameKey];
        self.score = [[dict objectForKey:kHighScoreScoreKey] integerValue];
        self.game = [[dict objectForKey:kHighScoreGameKey] integerValue];
        self.level = [[dict objectForKey:kHighScoreLevelKey] integerValue];
        self.date = [dict objectForKey:kHighScoreDateKey];
    }
    return (self);
}
- (NSDictionary *)putIntoDictionary;
{
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
                          name, kHighScoreNameKey,
                          [NSNumber numberWithInt:score], kHighScoreScoreKey,
                          [NSNumber numberWithInt:game], kHighScoreGameKey,
                          [NSNumber numberWithInt:level], kHighScoreLevelKey,
                          date, kHighScoreDateKey,
                          nil];
    return dict;
}

And in my HighScoreTable class:
- (id) load
{
    NSString *path = [self getFilePath];

//  [self clear];
    NSDictionary *rootLevelPlistDict = [NSDictionary dictionaryWithContentsOfFile:path];
    int versNum = [[rootLevelPlistDict objectForKey:kHighScoreVersKey] integerValue];

    if (versNum == kHighScoreVersNum) {
        NSArray *insideArray = [rootLevelPlistDict objectForKey:kHighScoresKey];

        NSDictionary *dict;
        for (dict in insideArray) {
            HighScore *hs = [[HighScore alloc] initFromDictionary:dict];
            [highScoresList addObject:hs];
            [hs release];
        }
    }
    return sharedHighScoresSingleton;
}


- (void) save
{   
    if (!changed)
        return;
    NSString *path = [self getFilePath];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:kNumberOfHighScores];
    for (HighScore *hs in highScoresList) {
        NSDictionary *dict = [hs putIntoDictionary];
        [array addObject:dict];
        [dict release];
    }
    NSDictionary *rootLevelPlistDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
            [[[NSBundle mainBundle] infoDictionary]
                objectForKey:(NSString*)kCFBundleNameKey], kHighScoreAppNameKey,
                [NSNumber numberWithInt:kHighScoreHeaderVersNum], kHighScoreHeaderVersKey,
                [NSNumber numberWithInt:kHighScoreVersNum], kHighScoreVersKey,
                [NSDate date], kHighScoreCreationKey,
             array, kHighScoresKey,
             nil];

    if (![rootLevelPlistDict writeToFile:path atomically:YES])
        NSLog(@"not successful in writing the high scores");
    [rootLevelPlistDict release];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!