所以我有一个plist结构化的字符串,该字符串是动态获取的(不是从文件系统获取的)。我将如何将该字符串转换为NSDictionary。

我尝试将其转换为NSData,然后使用NSPropertyListSerialization转换为NSDictionary,但是当我尝试访问NSDictionary时,它返回“[NSCFString objectAtIndex:]:无法识别的选择器发送到实例0x100539f40”,这表明未成功创建字典。

NSString的示例(即plist数据):

<?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>

谢谢!

最佳答案

参见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];
}

关于objective-c - 将Plist(NSString)解析为NSDictionary,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1072308/

10-12 04:25