christo16 christo16 - 1 year ago 166
Objective-C Question

Parse Plist (NSString) into NSDictionary

So I have a plist structured string, that get dynamically (not from the file system). How would I convert this string to a NSDictionary.

I've tried converting it NSData and then to a NSDictionary with NSPropertyListSerialization, but it returns "[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x100539f40" when I attempt to access the NSDictionary, showing that my Dictionary was not successfully created.

Example of the NSString (that is the plist data):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">


Answer Source

See Serializing a Property List

NSData* plistData = [source dataUsingEncoding:NSUTF8StringEncoding];
NSString *error;
NSPropertyListFormat format;
NSDictionary* plist = [NSPropertyListSerialization propertyListWithData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
NSLog( @"plist is %@", plist );
    NSLog(@"Error: %@",error);
    [error release];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download