objective-c 将 Plist (NSString) 解析为 NSDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1072308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:50:42  来源:igfitidea点击:

Parse Plist (NSString) into NSDictionary

objective-cxmlcocoaparsingplist

提问by christo16

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

所以我有一个 plist 结构的字符串,它是动态获取的(不是来自文件系统)。我将如何将此字符串转换为 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.

我尝试将其转换为 NSData,然后使用 NSPropertyListSerialization 转换为 NSDictionary,但是当我尝试访问 NSDictionary 时,它返回“[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x100539f40”,表明我的字典没有成功创建。

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

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> 

Thanks!

谢谢!

回答by Peter N Lewis

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

回答by Marco Mustapic

Try this:

尝试这个:

NSData * data = [yourString dataUsingEncoding:NSUTF8StringEncoding];

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSDictionary * dict = (NSDictionary*)[NSPropertyListSerialization
                                      propertyListFromData:data
                                      mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                      format:&format
                                      errorDescription:&errorDesc];

回答by Peter Hosey

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.

我尝试将其转换为 NSData,然后使用 NSPropertyListSerialization 转换为 NSDictionary,但是当我尝试访问 NSDictionary 时,它返回“[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x100539f40”,表明我的字典没有成功创建。

No, it shows no such thing. What it shows is that you tried to treat a string as an array. You'd need to determine where in the plist you were trying to get an array and why there was a string where you expected an array—i.e., whether you created the plist incorrectly (puttinga string into it where you meant to put an array) or are examining it incorrectly (the presence of a string is correct; your subsequent expectation of an array is wrong).

不,它表明没有这样的事情。它显示的是您试图将字符串视为数组。您需要确定您试图在 plist 中的哪个位置获取数组,以及为什么在您期望数组的位置有一个字符串——即,您是否错误地创建了 plist(一个字符串放入您打算放置数组的位置) ) 或者正在错误地检查它(字符串的存在是正确的;您随后对数组的期望是错误的)。