Terminating app due to uncaught exception 'NSInvalidArgumentException'

Discussion in 'iOS Programming' started by lJoct03, Jan 24, 2012.

  1. lJoct03 macrumors member

    Joined:
    Nov 18, 2009
    #1
    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
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    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 :)
     
  3. PhoneyDeveloper, Jan 25, 2012
    Last edited by a moderator: Jan 25, 2012

    PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. lJoct03 thread starter macrumors member

    Joined:
    Nov 18, 2009
    #4
    how do i find the exact location where the runtime exception occurs?
     
  5. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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.
     
  6. lJoct03 thread starter macrumors member

    Joined:
    Nov 18, 2009
    #6
    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];
        
        }
    }
     
  7. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    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?
     
  8. lJoct03, Jan 25, 2012
    Last edited by a moderator: Jan 26, 2012

    lJoct03 thread starter macrumors member

    Joined:
    Nov 18, 2009
    #8
    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;
    }
    
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    What is the type of the first eight objects you are adding to listOfItems?
     
  10. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    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?
     
  11. lJoct03 thread starter macrumors member

    Joined:
    Nov 18, 2009
    #11
    I have no idea! Should i change the strings to dictionaries? If so, how? Thanks!!
     
  12. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    "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 :)
     

Share This Page