Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lJoct03

macrumors member
Original poster
Nov 18, 2009
31
0
How do i fix this? Thanks!!

DEBUGGER OUTPUT:

2012-01-24 16:58:00.286 The Edge[7614:f803] -[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x1da9c
2012-01-24 16:58:00.298 The Edge[7614:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x1da9c'
*** First throw call stack:
(0x13df052 0x1570d0a 0x13e0ced 0x1345f00 0x1345ce2 0x3ca8 0x210f2b 0x2122cf 0x214edb 0x214f9c 0xb7836 0xc7f90 0x3a66 0xfb0ad 0x25466c 0x256010 0x3614a 0x36461 0x357c0 0x44743 0x451f8 0x38aa9 0x12c9fa9 0x13b31c5 0x1318022 0x131690a 0x1315db4 0x1315ccb 0x352a7 0x36a9b 0x271d 0x2695)
terminate called throwing an exception
 
It's nearly saying exactly what your error is ->
-[__NSCFConstantString objectForKey:]
Apparently you want to do objectForKey on a Dictionary, but it's trying to do it on a String.. So it's like, BOOM CRASH *firework sparkles*
So without knowing where it goes wrong, can't say much more :)
 
I think you didn't show the exact line where the runtime exception occurs but apparently the objects in your array are strings and not dictionaries and your code is treating them like dictionaries. You need to fix that up.
 
Last edited by a moderator:
how do i find the exact location where the runtime exception occurs?
 
You can step through the code until the exception is thrown.

You can set a breakpoint that's hit when any exception is thrown. In Xcode 4 go to the Breakpoints navigator. Click on the plus button at the bottom of the screen and choose Add Exception Breakpoint.
 
The bolded line is where the breakpoint stopped

Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (searching)
        return [copyListOfItems count];
    else {
    
    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary = [listOfItems objectAtIndex:section];
    [B]NSArray *array = [dictionary objectForKey:@"News"];[/B]
    return [array count];
    
    }
}
 
Right. So the exception tells you that the item in the listOfItems array is a NSString, not an NSDictionary.

So how do you create listOfItems?
 
As a NSMutableArray?? Can you explain this to me please? I feel like I'm almost there but not quite yet. Haha sorry. Thanks!!

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//Initialize the array.
	listOfItems = [[NSMutableArray alloc] init];
	
	//Add items
	[listOfItems addObject:@"News"];
	[listOfItems addObject:@"News"];
	[listOfItems addObject:@"News"];
	[listOfItems addObject:@"News"];
	[listOfItems addObject:@"News"];
	[listOfItems addObject:@"News"];
	[listOfItems addObject:@"News"];
	[listOfItems addObject:@"News"];
    
    NSMutableArray *todayArray = [NSArray arrayWithObjects:@"New Gym Rules", @"Dr.Freedman's Retirement", @"Mrs. Monseliu's Retirement", @"New Security Cameras", nil];
    NSDictionary *todayDict = [NSDictionary dictionaryWithObject:todayArray forKey:@"News"];
    
    NSMutableArray *yesterdayArray = [NSArray arrayWithObjects:@"News", @"News", @"News", @"News", nil];
    NSDictionary *yesterdayDict = [NSDictionary dictionaryWithObject:yesterdayArray forKey:@"News"];
    
    [listOfItems addObject:todayDict];
    [listOfItems addObject:yesterdayDict];
    
    //Initialize the copy array.
	copyListOfItems = [[NSMutableArray alloc] init];
	
	//Set the title
	self.navigationItem.title = @"News";
    
    //Add the search bar
    self.tableView.tableHeaderView = searchBar;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    
    searching = NO;
    letUserSelectRow = YES;
}
 
Last edited by a moderator:
What is the design of your data model for the table view? How many sections and rows should there be? I'm guessing it's meant to be an array of dictionaries of arrays, which would be a common design.

Why are you adding both strings and dictionaries to listOfItems?
 
What is the design of your data model for the table view? How many sections and rows should there be? I'm guessing it's meant to be an array of dictionaries of arrays, which would be a common design.

Why are you adding both strings and dictionaries to listOfItems?

I have no idea! Should i change the strings to dictionaries? If so, how? Thanks!!
 
"I have no idea" is not a good answer, but was kind of what I was expecting.

You need to understand what a data model is and you need to create one that matches your goals and design. You didn't answer how many sections there should be.

I don't know if I can or should answer every little question you have in order to implement this but I'll give you a little more.

I assume that your design (wherever it comes from) is an array of dictionaries of arrays. Each dictionary represents a section. The row array in each dictionary represents the title of each row in that section. To build this you create a mutable array. Then create the dictionary and array for each section and add them to the sections array. Your code does this, adding to section dictionaries, but also adds strings to the sections array. Don't add those strings to the sections array. If you make that change most or all of the rest of the code you show will probably work. If you really wanted to have more than two sections I'm going to recommend that you don't do that until you get the first two sections working.

Good luck :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.