Haechan Lee
Haechan Lee

Reputation: 35

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?

Upvotes: 1

Views: 1311

Answers (1)

jlehr
jlehr

Reputation: 15597

You need to specify NSDocumentDirectory rather than NSDocumentationDirectory.

Upvotes: 3

Related Questions