Search code examples
iosjsonuitableviewnsdata

SIGABRT terminating an app due to uncaught exception


I'm trying to retrieve data from web service and then filter the data and store it in another array based on a value. However, I get the following error whenever I try to filter it:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary Month]: unrecognized selector sent to instance 0x174079640'

This is my code:

- (void)getDataHTTPClient:(Client *)client didUpdateWithData:(id)data
{
    [self.view setUserInteractionEnabled:YES];

    if (isPullRefresh)
    {
        isPullRefresh = NO;
        [datasource removeAllObjects];
        [self.pullToRefreshView finishLoading];
    }

    NSMutableArray *array = (NSMutableArray *)data;
    if (![array count])
    {
        isNewsEnded = YES;
        [self.tableView reloadData];
        return;
    }
    [hud hide:YES];

    [datasource addObjectsFromArray:(NSMutableArray *) data];
    NSDate *currentDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar         components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
    monthInt= [components month];
    NSLog(@"month %li", monthInt);
    NSLog(@"month after deduction %li", monthInt-1);

    monthString = [NSString stringWithFormat:@"%zd",monthInt];

    for (EventsBean *item in datasource)
    {
        if([item.Month isEqualToString:monthString])
        {
            [filteredArray addObject:item];
        }
    }
    [self.tableView reloadData];

    if ([datasource count])
    {
        [self displayNoRecordMSG:NO];
    }
    else
    {
        [self displayNoRecordMSG:YES];
    }
}

the error shows on this line

 if([item.Month isEqualToString:monthString])
{
    [filteredArray addObject:item];
}

Solution

  • The data you received over the network will most likely not be an instance of EventsBean class. From the error log, it looks like it is actually a NSDictionary object. I'm guessing it is already parsed from a JSON string for you. So I would probably try to access the month property by doing item[@"Month"] instead.