NSDictionary writetofile无效

时间:2013-02-04 14:59:08

标签: ios objective-c nsdictionary

我正在尝试使用NSDictionary writetofile保存一些用户设置,但它无法正常工作。

我已经检查了类似的问题,但我无法找出问题所在。

这是我的代码:

NSDictionary *settings;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *settingsPath = [NSString stringWithFormat:@"%@/UserSettings.plist", [paths objectAtIndex:0]];
BOOL settingsExist = [[NSFileManager defaultManager] fileExistsAtPath:settingsPath];
if(settingsExist){
    NSLog(@"file found!");
    settings = [NSDictionary dictionaryWithContentsOfFile:settingsPath];
}else{
    NSLog(@"file not found!");
    settings = [[NSDictionary alloc] initWithObjectsAndKeys:@"iPhone",@"device", nil];
    [[NSFileManager defaultManager] createFileAtPath:settingsPath contents:nil attributes:nil];
    if([settings writeToFile:settingsPath atomically:YES]){
        NSLog(@"success!");
    }else{
        NSLog(@"fail!");
    }
}

它不断返回'失败!'。 你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

您需要指定NSDocumentDirectory而不是NSDocumentationDirectory