justintime
justintime

Reputation: 372

Not able to write NSDictionary to plist file

Update: I am able to write tot he file when I change the following line

[infoDict setObject:userList forKey:@"users"];

to

[infoDict setObject:@"sampleString" forKey:@"users"];

so, there must be problem with the structure I'm trying to write to the file, any thoughts?


I am trying to create a plist file and write to it at runtime in my iOS app. I am not able to write to it but I'm able to read though. The writeToFile method returns a 'NO'. When I manually try to manually open the file I created using Finder, it says "The data couldn’t be read because it isn’t in the correct format.", which I understand because on opening the file using a text editor it is empty and doesn't show any xml tags which a file of type plist should have. And I know I can read from it because on manually dragging a 'valid' plist file in the folder, my code is able to read an empty list from there.

1) Why isn't it creating a valid plist file?

and,

2) Why am I not able to write to (even a valid) plist file?

    NSLog(@"* Writing contents to plist file *");

    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* plistPath = nil;

    if ((plistPath = [path stringByAppendingPathComponent:@"users.plist"]))
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];

        BOOL exists = [fileManager fileExistsAtPath:plistPath];
        NSLog(exists ? @"yes, file exists": @"no, file doesnt exist");

        if (!exists) {
            NSLog(@"creating file since it doesnt exist");
            [fileManager createFileAtPath:plistPath contents:nil attributes:nil];
        }

        NSLog(@"path of file: %@", plistPath);
        NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] init];
        NSLog(@"infoDict: %@", infoDict);

        [infoDict setObject:userList forKey:@"users"];
        NSLog(@"infoDict: %@", infoDict);

        BOOL wrote = [infoDict writeToFile:plistPath atomically:YES];
        NSLog(wrote ? @"yes, wrote to the file": @"no, didn't write to file");
   }
    NSLog(@"--------------------------------------------");

Following is the output in the console:

    2014-10-17 10:50:11.290 UserReggy[1228:10032] * Writing contents to plist file *
2014-10-17 10:50:11.291 UserReggy[1228:10032] yes, file exists
2014-10-17 10:50:11.291 UserReggy[1228:10032] path of file: /Users/rvyas1/Library/Developer/CoreSimulator/Devices/DB612284-F481-467A-BAE8-37750497F42A/data/Containers/Data/Application/C9620C4D-EF1F-4C17-8D28-4E9B3B41F2C2/Documents/users.plist
2014-10-17 10:50:11.291 UserReggy[1228:10032] infoDict: {
}
2014-10-17 10:50:11.291 UserReggy[1228:10032] infoDict: {
    users =     {
        dfgdfgd = "<User: 0x7ffd884da820>";
    };
}
2014-10-17 10:50:11.292 UserReggy[1228:10032] no, didn't write to file
2014-10-17 10:50:11.294 UserReggy[1228:10032] --------------------------------------------

Upvotes: 0

Views: 2715

Answers (3)

Akshay Bhalotia
Akshay Bhalotia

Reputation: 829

NSMutableDictionary *fileData;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath: path])
    {
        fileData = [[NSMutableDictionary alloc] init];
        if ([fileData initWithContentsOfFile: path])
        {
            fileData = [fileData initWithContentsOfFile: path];
        }

    }
    else
    {
        [fileManager createFileAtPath:path contents:nil attributes:nil];

        fileData = [[NSMutableDictionary alloc] initWithCapacity:0];

    }

        [fileData setObject:data forKey:qid];

    BOOL wrote = [fileData writeToFile:path atomically:YES];
    NSLog(wrote ? @"yes, wrote to the file": @"no, didn't write to file");

Try this. Worked for me.

Upvotes: 0

Sunny Shah
Sunny Shah

Reputation: 13020

the problem is in line NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

it will give you null object beacause your plistPath is nil

Upvotes: 1

Feng Lin
Feng Lin

Reputation: 593

You can check your data in userList , it must be property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) , doc in the NSMutableDictionary Reference

Upvotes: 1

Related Questions