简体   繁体   中英

NSDictionary writetofile is not working

I'm trying to save some user settings using NSDictionary writetofile but it is not working.

I have already checked out similar questions but I could not find out what the problem is.

here's my code:

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!");
    }
}

It keeps returning 'fail!'. Could you help me with this please?

您需要指定NSDocumentDirectory而不是NSDocumentationDirectory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM