setObject不在其中一个类中工作

时间:2013-01-29 09:14:24

标签: ios nsmutabledictionary writetofile

我正在尝试使用实现创建一个'saveState'方法(在blablabla.m中):

-(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不是nill,在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”]是hill。我试图用@“test”替换'masterCode',但没有任何意义。

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

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,但不知道解释。解决方案是取代

NSMutableDictionary *plistCode = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath] 

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

之后该方法效果很好!感谢init)