Reputation: 63
I need to get a count of the objects in array inside of NSDictionary. For example:
po _dictionary
{
keys = (
"one",
"two"
);
}
Taking in consideration this is an array : [_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'
Upvotes: 0
Views: 1524
Reputation: 432
NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(@"%d",[[res valueForKey:@"result"] count]);
Upvotes: 0
Reputation: 1097
NSLog(@"%d", [[_dictionary objectForKey:@"keys"] count]);
As your [_dictionary objectForKey:@"keys"]
is an array you can get the count directly.
Upvotes: 0
Reputation: 13814
NSDictionary
has a method to get a NSArray
of keys which I find is much more readable than using objectForKey:
[[_dictionary allKeys] count]
Upvotes: 0
Reputation: 6187
[_dictionary objectForKey:@"keys"]
is an NSArray
and not an NSDictionary
. Therefore, it doesn't understand the allKeys
method. Drop it and it should work:
[[_dictionary objectForKey:@"keys"] count];
Upvotes: 3