[英]How to get count of objects in NSArray inside of NSDictionary
I need to get a count of the objects in array inside of NSDictionary. 我需要对NSDictionary内部数组中的对象进行计数。 For example: 例如:
po _dictionary
{
keys = (
"one",
"two"
);
}
Taking in consideration this is an array : [_dictionary objectForKey:@"keys"]
考虑到这是一个数组: [_dictionary objectForKey:@"keys"]
my question is how can I get the count in the array? 我的问题是如何获取数组中的计数?
I try this : 我尝试这样:
[[[_tutorials objectForKey:@"keys"] allKeys ]count];
but I'm getting this error: 但我收到此错误:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray allKeys]: unrecognized selector sent to instance 0xa419ea0' *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFArray allKeys]:无法识别的选择器发送到实例0xa419ea0'
[_dictionary objectForKey:@"keys"]
is an NSArray
and not an NSDictionary
. [_dictionary objectForKey:@"keys"]
是一个NSArray
而不是NSDictionary
。 Therefore, it doesn't understand the allKeys
method. 因此,它不了解allKeys
方法。 Drop it and it should work: 删除它,它应该可以工作:
[[_dictionary objectForKey:@"keys"] count];
NSLog(@"%d", [[_dictionary objectForKey:@"keys"] count]);
由于您的[_dictionary objectForKey:@"keys"]
是一个数组,因此您可以直接获取计数。
干得好:
NSLog(@"%lu", _dictionary[@"keys"].count);
NSDictionary
有一种获取NSArray
键的方法,我发现它比使用objectForKey:
更具可读性objectForKey:
[[_dictionary allKeys] count]
NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(@"%d",[[res valueForKey:@"result"] count]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.