user3094099
user3094099

Reputation: 63

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. 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

Answers (5)

mahesh chowdary
mahesh chowdary

Reputation: 432

  NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];

  NSLog(@"%d",[[res valueForKey:@"result"] count]);

Upvotes: 0

Charan Giri
Charan Giri

Reputation: 1097

NSLog(@"%d", [[_dictionary objectForKey:@"keys"] count]);

As your [_dictionary objectForKey:@"keys"] is an array you can get the count directly.

Upvotes: 0

Pier-Luc Gendreau
Pier-Luc Gendreau

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

Abhi Beckert
Abhi Beckert

Reputation: 33369

Here you go:

NSLog(@"%lu", _dictionary[@"keys"].count);

Upvotes: 0

BergQuester
BergQuester

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

Related Questions