奇怪的开关解析错误

时间:2011-04-22 19:48:57

标签: objective-c

switch (indexPath.section) {  
  case 0: //products used  
    NSString * chemical = [selectedChemicals objectAtIndex:indexPath.row];  
    cell.textLabel.text = chemical;  
    break;  
  case 1: //areas sprayed  
    return [selectedAreas count];  
    break;  
  case 2://target pests  
    return [selectedPests count];  
    break;  
  case 3://notes  
    return 1;  
    break;  
}

给了我:“/ Users / grady / programming / ObjectivelyBetter / bioguard / Class / JobWizardViewController.m:147:错误:'化学'未申报(首次使用此功能)”

在案例开头放置一个空格分号可以修复它。

switch (indexPath.section) {  
  case 0: //products used  
    ;  
    NSString * chemical = [selectedChemicals objectAtIndex:indexPath.row];  
    cell.textLabel.text = chemical;  
    break;  
  case 1: //areas sprayed  
    return [selectedAreas count];  
    break;  
  case 2://target pests  
    return [selectedPests count];  
    break;  
  case 3://notes  
    return 1;  
    break;  
} 

1 个答案:

答案 0 :(得分:2)

当你在case语句中声明变量时,将语句括在大括号内是一种很好的做法(并且需要避免这些类型的错误),例如。

case 0:
    {
        int i = 0;
        ....
        break;
    }

不确定为什么分号会“解决”这个问题。这有点奇怪......花括号是你需要的。

在您的特定情况下,您也可以只消除局部变量声明并设置单元格textLabel,如下所示:

  cell.textLabel.text = [selectedChemicals objectAtIndex:indexPath.row];