1

我正在尝试使用 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!");
    }
}

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

4

1 回答 1

3

您需要指定NSDocumentDirectory而不是NSDocumentationDirectory.

于 2013-02-04T15:02:26.683 回答