繁体   English   中英

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSCFString nric]:

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

当我使用刚刚实现的搜索栏时出现此错误。有些字母可以正常工作,而其他字母会因标题中的错误而崩溃。 错误似乎在这里,但我无法弄清楚它有什么问题:“cell.textLabel.text = info.nric;”。

有人请帮忙=(

- (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;
}

您认为仅包含PatientInfo对象的 arrays 之一实际上包含一个 NSString。 因此,当您编写info.nric时,它会询问 NSString 的nric属性,这当然不存在。 实际错误将是您错误地将字符串放入数组中( copyListOfItemslistOfItems )。

替换为以下代码,您将知道代码中到底出了什么问题:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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