为什么我的iOS数据不能与NSKeyedArchiver持久存在?

时间:2012-12-18 23:01:32

标签: iphone objective-c ios save nskeyedarchiver

我正在研究我的第一款iPhone游戏的“最后润色”。出于某种原因,当我使用NSKeyedArchiver / Unarchiver保存时,数据似乎加载一次然后丢失或其他东西。

我正在尝试使用相同的归档程序保存2个对象:NSMutableDictionary levelsPlistNSMutableArray categoryLockStateArray。设置为非原子,保留头文件中的属性。

以下是我能够推断的内容:

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

  2. 当我在此viewController中保存,弹出到上一个,然后推送回到这个,数据被保存并打印出我想要的它来。

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

    < / LI>

    知道为什么会这样吗?我有这个设置错了吗?几个月前我从一本书中复制了它。这是我用来保存和加载的方法。

        - (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方法调用的。

    感谢您提供的任何帮助!

0 个答案:

没有答案