Number of NSDictionary

How can I calculate how much I have inside the โ€œaccountsโ€?

bills = ( { id = 1; name = "Cursus Nibh Venenatis"; value = "875.24"; }, { id = 2; name = "Elit Fusce"; value = "254.02"; } ); 

I reckon so:

 NSUInteger keyCount = [resultsDictionary count]; NSLog(@"%i", keyCount); 

Thanks!

+4
source share
4 answers

The naive solution assumes that the OP wants to consider bills , which turns out to be an array, so the solution will be

 NSLog(@"Count: %i", [[resultsDictionary objectForKey: @"bills"] count]); 

However, if you have a dictionary with several objects that you want to count, then listing them all is the only way to go.

+6
source
 NSUInteger keyCount = [resultsDictionary count]; NSLog(@"%i", keyCount); 

true, but you can also use [resultsDictionary allKeys]; which will return an array of keys, and you can directly get its score. For more information, visit docs : v

+4
source

NSUInteger count = [bills count]; // top-level elements in the dictionary

If you want to get ALL items, you need to use the enumeration of blocks and ask each dictionary recursively about what an account is, and then summarize. You must write the code - there is no method provided by the system for this. Look at the block enumerations (i.e. list ....) in the class reference.

you can also ask each element what it is, and if "isKindOf:" is NSDictionary or NSArray, get a counter, otherwise treat it as 1.

+1
source
 NSMutableArray *billsArray =[[NSMutableArray alloc] init]; billsArray = [resultsDictionary valueForKey:@"bills"];//returns array NSUInteger keyCount = [billsArray count]; NSLog(@"%i", keyCount); 

I think it will be useful for you.

+1
source

All Articles