0
votes

Variables:

NSMutableDictionary *rootObj;
NSDictionary *innerDict;
NSString *name;
NSArray *scores;

Code:

rootObj = [NSMutableDictionary dictionaryWithCapacity:2];

    scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:6],
              [NSNumber numberWithFloat:4.6], [NSNumber numberWithLong:6.0000034], nil];
    name = @"George Washington";



    innerDict = [NSDictionary dictionaryWithObjects:
                 [NSArray arrayWithObjects: name, scores, nil]
                                            forKeys:[NSArray arrayWithObjects:@"Name", @"Scores", nil]];
    [rootObj setObject:innerDict forKey:@"Washington"];

    scores = [NSArray arrayWithObjects:[NSNumber numberWithInt:8],
              [NSNumber numberWithFloat:4.9],
              [NSNumber numberWithLong:9.003433], nil];
    name = @"Abraham Lincoln";


    innerDict = [NSDictionary dictionaryWithObjects:
                 [NSArray arrayWithObjects: name, scores, nil]
                                            forKeys:[NSArray arrayWithObjects:@"Name", @"Scores", nil]];
    [rootObj setObject:innerDict forKey:@"Lincoln"];

    id plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj
                                                          format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];

    // SERIALIZATION

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    NSData *xmlData;
    NSString *error;

    xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
                                                         format:NSPropertyListXMLFormat_v1_0
                                               errorDescription:&error];
    if(xmlData) {
        NSLog(@"No error creating XML data.");
        [xmlData writeToFile:path atomically:YES];
        NSFileManager *manger = [[NSFileManager alloc] init];
        NSLog(@"%@", [manger contentsAtPath:path]);
    }
    else {
        NSLog(@"error");
       // [error release];
    }

    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"%@", myDictionary);

The output is:

2013-01-18 14:33:50.066 PListTrials[11281:c07] No error creating XML data.
2013-01-18 14:33:50.067 PListTrials[11281:c07] (null)
2013-01-18 14:33:50.067 PListTrials[11281:c07] (null)

How is myDictionary null? Code pulled from: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/CreatePropListProgram/CreatePropListProgram.html#//apple_ref/doc/uid/10000048i-CH5-SW1

2

2 Answers

0
votes

Simple: you can't write to the application bundle. Change path to something writeable (e. g. the Documents directory).

Update:

Also,

xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
                                                     format:NSPropertyListXMLFormat_v1_0
                                           errorDescription:&error];

is superfluous. plist is already the XML data.

1
votes

You can't write on the application main bundle. Do it in Document bundle

Initially, your plist file "Data.plist" doesn't exist in the path, so you need to create it. Better to check if the file exists whenever you want to write data in. Try the following:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Data.plist"];
if ([fileManager fileExistsAtPath:path] == NO) {
    [fileManager createFileAtPath:path contents:YourContent attributes:nil];
}else {
    [Content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}