[英]Getting value from NSDictionary to NSMutableArray
我有这个NSDictionary:
Categorys = {
Category = (
{
categoryDescription = {
text = " Description";
};
categoryIconPath = {
text = " 7.jpg";
};
categoryId = {
text = " 25";
};
categoryName = {
text = " My Photos";
};
categoryStatus = {
text = " ON";
};
publicPrivate = {
text = " Private";
};
userId = {
text = " 2";
};
},
..................
我想将categoryName(例如此处的My Photos)放入我的NSArray中。
我怎样才能做到这一点?
我没办法
我已经从http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/尝试了此代码
NSString *category = [[[[xmlDictionary objectForKey:@"Categorys"] objectForKey:@"Category"] objectForKey:@"categoryName"] stringForKey:@"text"];
但它给出了错误
2012-03-27 15:30:35.320 File[1481:903] -[NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1462c0
在类别中,类别键所引用的对象是一个数组,您可以通过以下事实知道其内容用括号( ... )
括起来而不用大括号{ ... }
括起来。
查看您的代码并将其分解:
NSString *category = [[[[xmlDictionary objectForKey:@"Categorys"] // This is the outermost dictionary
objectForKey:@"Category"] // This is an NSArray
objectForKey:@"categoryName"] stringForKey:@"text"];
// This breaks NSArray doesn't respond to objectForKey:
对其进行分解,您需要:
NSDictionary* categorys = [xmlDictionary objectForKey:@"Categorys"];
NSArray* categoryArray = [categorys objectForKey: @"Category"];
NSDictionary* category = [categoryArray objectAtIndex: i]; // i is the index of the category you want e.g. 0
// etc
答案在错误消息中。 您从中获得的对象之一
NSString *category = [[[[xmlDictionary objectForKey:@"Categorys"] objectForKey:@"Category"] objectForKey:@"categoryName"] stringForKey:@"text"];
没有选择器objectForKey
。
现在,您需要分解代码并进行一些调试,以找出问题所在。
正在收到相同的错误“ [[NSCFArray objectForKey:]:无法识别的选择器发送到实例””
当我查看运行时从捆绑软件中加载的plist文件的结构时,似乎不太可能使用了错误的选择器。 问题是,当我运行该应用程序时,它不是从捆绑软件中提取原始plist,而是从我的apps目录中提取了一个保存的plist。 由于保存的plist与原始plist的结构不正确且完全不同,因此我确实使用了错误的选择器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.