简体   繁体   中英

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString nric]:

I get this error when i play with the searchbar that I have just implemented.. Some letters work while others will crash with the error in the title. The error seems to be here but i cannot figure out what is wrong with it: "cell.textLabel.text = info.nric;".

Someone please help =(

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

    if(searching){
        PatientInfo *info = [copyListOfItems objectAtIndex:indexPath.row];
        cell.textLabel.text = info.nric;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%i, %i", info.category, info.age];
    }
    else {

        //First get the dictionary object
        NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
        NSArray *array = [dictionary objectForKey:@"Patients"];
        PatientInfo *info = [array objectAtIndex:indexPath.row];
        // NSString *cellValue = [array objectAtIndex:indexPath.row];
        cell.textLabel.text = info.nric;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%i, %i", info.category, info.age];
    }

    return cell;
}

One of the arrays that you think contains only PatientInfo objects actually contains an NSString. So when you then write info.nric , it's asking that NSString for its nric property, which of course doesn't exist. The actual error would be wherever you're mistakenly putting a string in the array (either copyListOfItems or listOfItems ).

Replace with the following code, you will know what exactly is going wrong in your code:

PatientInfo *info = [copyListOfItems objectAtIndex:indexPath.row];
if((nil != info) && ([info isKindOfClass:[PatientInfo class]))
{
    if([info respondsToSelector:@selector(nric)])
    {
        cell.textLabel.text = info.nric;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM