Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  <plist version="1.0">  <dict>   <key>Key1</key>   <dict>    <key>Test1</key>    <false/>    <key>Key2</key>    <string>Value2</string>    <key>Key3</key>    <string>value3</string>   </dict>  </dict>  </plist>  

Thanks!

like image 781
christo16 Avatar asked Jul 02 '09 03:07

christo16


2 Answers

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 ); if(!plist){     NSLog(@"Error: %@",error);     [error release]; } 
like image 95
Peter N Lewis Avatar answered Sep 23 '22 03:09

Peter N Lewis


Try this:

NSData * data = [yourString dataUsingEncoding:NSUTF8StringEncoding];  NSString *errorDesc = nil; NSPropertyListFormat format; NSDictionary * dict = (NSDictionary*)[NSPropertyListSerialization                                       propertyListFromData:data                                       mutabilityOption:NSPropertyListMutableContainersAndLeaves                                       format:&format                                       errorDescription:&errorDesc]; 
like image 20
Marco Mustapic Avatar answered Sep 20 '22 03:09

Marco Mustapic