Counting allKeys from NSDictionary returns EXC_BAD_ACCESS

I am trying to send an NSDictionary to a TableViewController, the data originally comes from a .plist file. I just want to send an object that exists further down the hierarchy to a new TableViewController. But problems arise when I try to count the number of elements in numberOfSectionsInTableView.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Gets the dictionary for the currently selected row NSDictionary *dict = [[data objectForKey:[[data allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; // Checks if the key "Room" exists if([dict objectForKey:@"Room"]) { SalesFairSessionTableViewController *sessionViewController = [[SalesFairSessionTableViewController alloc] init]; // Sets the data in the subview Controller [sessionViewController setData:[dict objectForKey:@"Room"]]; // And the title [sessionViewController setTitle:[dict objectForKey:@"Title"]]; // Problem is here... returns EXC_BAD_ACCESS NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); [self.navigationController pushViewController:sessionViewController animated:YES]; [sessionViewController release]; } } 

If I just use allKeys as follows:

 NSLog([[dict objectForKey:@"Room"] allKeys]); 

It returns ("Position 1", "Item 2") in the console.

But when I add the "count" method as follows:

 NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); 

I just get: Program signal: "EXC_BAD_ACCESS".

What am I missing here?

+4
source share
2 answers
 NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); 

count gives an int , but you print with %@ , waiting for a pointer to an object. Use %d instead.

+10
source

Since your code is currently standing, you tell the NSLog line to expect an object. count returns an NSInteger , hence an error. To fix, change this line:

 NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); 

:

 NSLog(@"%i", [[[dict objectForKey:@"Room"] allKeys] count]); 
+3
source

Source: https://habr.com/ru/post/1333488/


All Articles