0

我正在尝试通过实现(在 blablabla.m 中)创建一个“saveState”方法:

-(void)saveState {
NSString *masterCode = [[NSString alloc] initWithString:first];
masterCode = [masterCode stringByAppendingString:second];
masterCode = [masterCode stringByAppendingString:third];
masterCode = [masterCode stringByAppendingString:fourth];
NSLog(@"master passcode - %@", masterCode);

NSString *plistPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"timeLimiterMasterCode.plist"];

if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    [[NSFileManager defaultManager] createFileAtPath:plistPath contents:nil attributes:nil];
}

if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    NSLog(@"file exists, %@", plistPath);
}

NSMutableDictionary *plistCode = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

[plistCode setObject:masterCode forKey:@"MasterCode"];

NSLog(@"whole plist - %@", plistCode);

[plistCode writeToFile:plistPath atomically: YES];

NSLog(@"master passcode from file - %@", [plistCode objectForKey:@"MasterCode"]);

}

masterCode 不是空的,在 blablabla.h 文件中我使用协议 UIApplicationDelegate,“文件存在”但应用程序返回:2013-01-29 12:19:50.496 timeLimiter[916:907] 整个 plist - (null) 2013-01 -29 12:19:50.500 timeLimiter[916:907] 文件中的主密码 - (null)

除了在 viewController (.m, .h) 中,我使用另一个 plist 和 write 方法并且工作正常。

我发现 [plistCode setObject:masterCode forKey:@"MasterCode"] 是山。我试图用@“test”替换'masterCode',但没有任何意义。

你能解决我的问题吗?请?

4

1 回答 1

0

我找到了解决方案,但不知道解释。解决方法是更换

NSMutableDictionary *plistCode = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath] 

NSMutableDictionary *newPlistCode = [[NSMutableDictionary alloc] init]. 

之后这种方法效果很好!感谢初始化)

于 2013-01-29T09:53:09.797 回答