1

我只是在做我的第一款 iPhone 游戏的“收尾工作”。出于某种原因,当我使用 NSKeyedArchiver/Unarchiver 保存时,数据似乎加载了一次,然后就丢失了。

我正在尝试使用相同的存档器保存 2 个对象: anNSMutableDictionary levelsPlist和 an NSMutableArray categoryLockStateArray。被设置为非原子,在头文件中保留属性。

这是我能够推断出的:

  1. 保存为 NSMutableDictionary 的对象始终存在。它工作得很好。

  2. 当我在这个 viewController 中保存时,弹出到前一个,然后回这个,数据被保存并按我想要的方式打印。

  3. 但是当我保存在这个 viewController 中,然后推送一个新的并回到这个 viewController 中时,categoryLockState 丢失了。

知道为什么会发生这种情况吗?我这个设置错了吗?我是几个月前从一本书上抄来的。这是我用来保存和加载的方法。

    - (void) saveGameData {
        NSLog(@"LS:saveGameData");

        // SAVE DATA IMMEDIATELY
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
        NSMutableData *gameSave= [NSMutableData data];
        NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameSave];
        [encoder encodeObject:self.categoryLockStateArray forKey:@"categoryLockStateArray];
        [encoder encodeObject:self.levelsPlist forKey:@"levelsPlist"];

        [encoder finishEncoding];
        [gameSave writeToFile:gameStatePath atomically:YES];

        NSLog(@"encoded catLockState:%@",categoryLockStateArray);
    }

- (void) loadGameData {
    NSLog(@"loadGameData");

    // If there is a saved file, perform the load
    NSMutableData *gameData = [NSData dataWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"gameState.dat"]];

    // LOAD GAME DATA
    if (gameData) {
        NSLog(@"-Loaded Game Data-");
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];
        self.levelsPlist = [unarchiver decodeObjectForKey:@"levelsPlist"];
        categoryLockStateArray = [unarchiver decodeObjectForKey:@"categoryLockStateArray"];

        NSLog(@"decoded catLockState:%@",categoryLockStateArray);
    }

    // CREATE GAME DATA
    else {
        NSLog(@"-Created Game Data-");
        self.levelsPlist = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLevelsPlist ofType:@"plist"]];
    }

    if (!categoryLockStateArray) {
        NSLog(@"-Created categoryLockStateArray-");

        categoryLockStateArray = [[NSMutableArray alloc] initWithCapacity:[[self.levelsPlist allKeys] count]];

        for (int i=0; i<[[self.levelsPlist allKeys] count]; i++) {
            [categoryLockStateArray insertObject:[NSNumber numberWithBool:FALSE] atIndex:i];
        }
    }

    // set the properties of the categories
    self.categoryNames = [self.levelsPlist allKeys];
    NUM_CATEGORIES = [self.categoryNames count];
    thisCatCopy = [[NSMutableDictionary alloc] initWithDictionary:[[levelsPlist objectForKey:[self.categoryNames objectAtIndex:pageControl.currentPage]] mutableCopy]];
    NUM_FINISHED = [[thisCatCopy objectForKey:kNumLevelsBeatenInCategory] intValue];
}

我只能猜测它与 viewController 在弹出然后推回时被卸载并重新加载的事实有关,但是我的 viewDidLoad 代码没有提及这些变量中的任何一个。它们是从 viewDidAppear 方法调用的。

谢谢你尽你所能的帮助!

4

0 回答 0