Search code examples
objective-ccocoa-touchnsarraynsdictionarynslog

Count of array inside NSDictionary is not logged although the array itself is logged


I'm, working with an NSMutableArray called sections to store the section data for my UITableView in.

Each section is an NSDictionary containing the following items: Stuff, another array to hold the data rows of the section, and Header, a string for the section's header title.

The problem I am having is trying to get the count of the Stuff array.

NSLog(@"Returning the count of %@", [[[sections objectAtIndex:0] objectForKey:@"Stuff"] count]); 

I have been able to confirm that [[sections objectAtIndex:0] objectForKey:@"Stuff"] does in fact get me the array; if I do this

NSLog(@"returning the contents of %@", [[sections objectAtIndex:0] objectForKey:@"Stuff"]);

the contents are logged.

I also know that normally [Stuff count] would happily return the count of the array.

Am I missing something here or is it different because the array is in a dictionary?


Solution

  • The dictionary doesn't know what kind of objects it hold so you must tell it by either assigning to an NSMutableArray or in your case, casting the object returned from your dictionary to NSMutableArray *

    NSLog(@"Returning the count of %@", [(NSArray *)[[sections objectAtIndex:0] objectForKey:@"Stuff"] count]);