我試圖用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!");
}
}
它保留返回 '失敗!'。 你能幫我解決這個問題嗎?
我確定你的意思是'NSDocumentDirectory'而不是'NSDocumentationDirectory'。另外,您不得***使用格式字符串來構建文件路徑。爲此,使用'stringByAppendingPathComponent:'。 – 2013-02-04 15:02:00
@ H2CO3啊,終於搞定了!謝謝,我會記住這一點。謝謝:D –