Bob N.
Bob N.

Reputation: 43

Objective-c read plist to NSDictionary

I see that this method is deprecated:

[[NSDictionary alloc] initWithContentsOfFile: url.path];

If I'm trying to read in the contents of a plist to create this dict, does anyone know what the proper new way is to do it?

Thanks!

Upvotes: 1

Views: 1430

Answers (1)

vadian
vadian

Reputation: 285250

Nowadays it's highly recommended to use NSPropertyListSerialization

NSURL *url = [NSURL fileURLWithPath:@"/Users/myUser/path/to/foo.plist"];
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:data options:0 format:nil error:&error];
if (error) {
    // handle error
} else {
    NSLog(@"%@", dictionary);
}

Upvotes: 4

Related Questions