Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary Count

how can I count how many itens do I have inside of "bills"?

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

I'm counting this way:

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

Thanks!

like image 239
Lucas Veiga Avatar asked Jul 31 '12 13:07

Lucas Veiga


3 Answers

A naive solution would assume the OP wants to count bills, which happens to be an array, so the solution would be

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

However, if you have a dictionary with more than one object you want to count, then enumerating them all is the only way to go.

like image 164
Henri Normak Avatar answered Nov 19 '22 14:11

Henri Normak


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

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

like image 4
ilhnctn Avatar answered Nov 19 '22 14:11

ilhnctn


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

I think it will be helpful to you.

like image 1
Prasad G Avatar answered Nov 19 '22 15:11

Prasad G