ios 在 NSDictionary 中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7023968/
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
get value in NSDictionary
提问by hiwordls
I want to get value from NSDictionary
. I want to control "Code" key according to Code key, I will get datum values. I get error message When I try different methods.
我想从NSDictionary
. 我想根据代码键控制“代码”键,我会得到数据值。当我尝试不同的方法时收到错误消息。
ERROR MESSAGE
错误信息
2011-08-11 12:45:21.549 AOK[6312:207] -[__NSArrayM getObjects:andKeys:]: unrecognized selector sent to instance 0x5e138b0 2011-08-11 12:45:21.550 AOK[6312:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM getObjects:andKeys:]: unrecognized selector sent to instance 0x5e138b0' *Call stack at first throw:
2011-08-11 12:45:21.549 AOK[6312:207] -[__NSArrayM getObjects:andKeys:]:无法识别的选择器发送到实例 0x5e138b0 2011-08-11 12:45:21.5312应用程序[__NSArrayM:andKeys:] *OK2由于未捕获的异常 'NSInvalidArgumentException',原因:'-[__NSArrayM getObjects:andKeys:]: 无法识别的选择器发送到实例 0x5e138b0' *第一次抛出时调用堆栈:
NSDictionary
NSD字典
(
{
Omschrijving = "ADDD";
Ophaaldata = {
Datum = (
"2011-07-04",
"2011-07-11",
"2011-07-18",
"2011-07-25"
);
};
},
{
Omschrijving = "BBBB";
Ophaaldata = {
Datum = (
"2011-07-05",
"2011-07-12",
"2011-07-19",
"2011-07-26"
);
};
},
{
Omschrijving = "KKKK";
Ophaaldata = {
Datum = (
"2011-07-07",
"2011-07-14",
"2011-07-21",
"2011-07-28"
);
};
},
{
Omschrijving = "LLLLL";
Ophaaldata = {
Datum = (
"2011-07-01",
"2011-07-08",
"2011-07-15",
"2011-07-22",
"2011-07-29"
);
};
},
{
Omschrijving = "MMMMM";
Ophaaldata = {
Datum = (
"2011-07-01",
"2011-07-15",
"2011-07-29"
);
};
},
{
Code = POP;
Omschrijving = "FFFF";
Ophaaldata = {
Datum = "2011-07-12";
};
},
{
Code = 00;
Omschrijving = "SSSS";
Ophaaldata = {
Datum = (
"2011-07-14",
"2011-07-27"
);
};
},
{
Code = 01;
Omschrijving = "CCCCC";
Ophaaldata = {
Datum = (
"2011-07-06",
"2011-07-20"
);
};
}
)
回答by Rudy Velthuis
What you display is an NSArray of NSDictionaries, each containing an NSString with the description (Omschrijving) and an NSDictionary (Ophaaldata) with one key (Datum), which is an array of dates. If you send Objects:andKeys:
to an NSArray, it won't work.
您显示的是一个 NSDictionaries 的 NSArray,每个包含一个带有描述 (Omschrijving) 的 NSString 和一个带有一个键 (Datum) 的 NSDictionary (Ophaaldata),它是一个日期数组。如果您发送Objects:andKeys:
到 NSArray,它将不起作用。
These are a few examples of how you can get at the individual items:
这些是您如何获得单个项目的一些示例:
NSArray *dicts = ...; // wherever you get the array
NSDictionary *mijnDict = [dicts objectAtIndex: n];
NSString *omschrijving = [mijnDict objectForKey: @"Omschrijving"];
NSDictionary *ophaaldata = [mijnDict objectForkey: @"Ophaaldata"];
NSArray *datum = [ophaaldata objectForkey: @"Datum"];
NSString *eersteDatum = [datum objectAtIndex: 0];
These are only examples of how you can address the items. But this is the structure your output shows. Anything else would be useless, anyway, since a dictionary can only contain each key once. I formatted the code a little, so you can see the structure a bit better.
这些只是您如何处理这些项目的示例。但这是您的输出显示的结构。无论如何,其他任何东西都是无用的,因为字典只能包含每个键一次。我稍微格式化了代码,所以你可以更好地看到结构。
回答by Paresh Navadiya
During Response Value of lines keep in mind
在线路的响应值期间记住
{ .......... } indicates dictionary.
(............) indicates array.
Dictionary always have key with it like somename = "value in it", .......
Array has value seperated by comma like a , b , c .....
Now it is possible it may have array into dictionary like { somekey = ( a,b,c) } and vice versa
回答by Andrea Bergia
reason: '-[__NSArrayM getObjects:andKeys:]:
Are you sureyou have an NSDictionary
and not an NSArray
?
你确定你有一个NSDictionary
而不是一个NSArray
?