簡體   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