我正在尝试使用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!");
}
}它总是返回'fail!‘。你能帮我拿一下这个吗?
发布于 2013-02-04 23:02:27
您需要指定NSDocumentDirectory而不是NSDocumentationDirectory。
https://stackoverflow.com/questions/14689406
复制相似问题