用来自 NSdictionary 的数据初始化 NSArray [英] Initialize NSArray with data from NSdictionary
问题描述
我从 URL 接收数据,将其放入 NSDictionary,然后我需要为每个标签创建 NSArray.我的 NSDictionary 看起来像:
I receiving data from URL, put it in NSDictionary, and then I need to create NSArray for each tags. My NSDictionary looks like:
(
{
id = {
text = 1;
};
logo = {
text = "url 1";
};
name = {
text = "some name 1";
};
},
{
id = {
text = 2;
};
logo = {
text = "url 2";
};
name = {
text = "some name 2";
};
},
{
id = {
text = 3;
};
logo = {
text = "url 3";
};
name = {
text = "some name 3";
};
}
)
我想要这样的数组:arrLogo = (@"url 1", @"url 2", "url 3").
I want to have array like that: arrLogo = (@"url 1", @"url 2", "url 3").
我接下来要做的是:arrName=[[dict objectForKey:@"name"] valueForKey:@"text"];
但是 Xcode 给了我一个错误:*** 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[__NSArrayM objectForKey:]:无法识别的选择器发送到实例
But Xcode gives me an error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance
我试图初始化数组的代码有问题,看起来我的字典由数组组成.如何正确提取数据?
Problem in the code that I'm trying to initialize an arrays, it's look like that my dict consist of arrays. How do I extract the data correctly?
推荐答案
为此,您必须遍历您拥有的 Array 对象.您可以使用快速枚举作为
For this you have to iterate through object of Array that you have. You can use fast enumeration as
for(object in JsonArray){
NSString *value = [object valueForKey:@"logo"]valueForKey:@"text"]];
//Add this value to your array
}
}
希望这会有所帮助.
这篇关于用来自 NSdictionary 的数据初始化 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!