I'm encountering an extremely vexing problem. I have a UITableViewController which in its init method decodes a dictionary from a JSON or plist file (I've tried both), then retrieves an array from that dictionary. Later on, in the method tableView:numberOfRowsInSection:, I'm returning the count of that array.
However, for reasons beyond me, calling count on the array at that point crashes the application, though calling count directly after assignment in init doesn't. Also, if I replace the initial assignment with a programmatically created array (via NSArray initWithObjects), it works fine.
JSON decoding in init:
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"json"];
SBJSON *jsonParser = [SBJSON new];
NSDictionary* dict = [jsonParser objectWithString:[NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil]];
categories = [dict objectForKey:@"ContentCategories"];
// Outputs correct count
NSLog(@"Count: %@", [NSNumber numberWithInt:[categories count]]);
Programmatic init:
categories = [[NSArray alloc] initWithObjects: [[NSDictionary alloc] initWithObjectsAndKeys:@"Junk", @"Title"]];
// Outputs correct count
NSLog(@"Count: %@", [NSNumber numberWithInt:[categories count]]);
UITableViewController number of rows method:
// Outputs correctly if programmatically created, crashes if decoded from JSON/plist
NSLog(@"Count: %@", [NSNumber numberWithInt:[categories count]]);
I've tried the "categories" variable as an ivar, a propertied-ivar, and as a class variable, with no luck.
Thanks for your help!