【问题标题】:Expected expression Error in switch function - Objective-C开关函数中的预期表达式错误 - Objective-C
【发布时间】:2014-05-05 15:32:07
【问题描述】:

我正在学习 switch 功能,所以我将 if 和 else 语句更改为这样:

但它给了我“预期的表达”错误。

为什么?

switch (indexPath.section) {

    case 0: return {
        cell.textLabel.text = FileANames[indexPath.row];
        cell.detailTextLabel.text = FileADetails[FileANames[indexPath.row]];
    }

    case 1: return {
        cell.textLabel.text = FileBNames[indexPath.row];
        cell.detailTextLabel.text = FileBDetails[FileBNames[indexPath.row]];
    }

    case 2: return {
        cell.textLabel.text = FileCNames[indexPath.row];
        cell.detailTextLabel.text = FileCDetails[FileCNames[indexPath.row]];
    }

    default: return cell;
}

//这个是对的

if (indexPath.section == 0) {
    cell.textLabel.text = FileANames[indexPath.row];
    cell.detailTextLabel.text = FileADetails[FileANames[indexPath.row]];
}

else if (indexPath.section == 1) {
    cell.textLabel.text = FileBNames[indexPath.row];
    cell.detailTextLabel.text = FileBDetails[FileBNames[indexPath.row]];
}

else if (indexPath.section == 2) {
    cell.textLabel.text = FileCNames[indexPath.row];
    cell.detailTextLabel.text = FileCDetails[FileCNames[indexPath.row]];
}

return cell;
}

谢谢。

【问题讨论】:

  • 不要这样return。也可以break
  • 谢谢!现在可以了。

标签: objective-c if-statement switch-statement


【解决方案1】:

应该是这个

switch (indexPath.section) {

    case 0:  {
        cell.textLabel.text = FileANames[indexPath.row];
        cell.detailTextLabel.text = FileADetails[FileANames[indexPath.row]];
        break;
    }

    case 1: {
        cell.textLabel.text = FileBNames[indexPath.row];
        cell.detailTextLabel.text = FileBDetails[FileBNames[indexPath.row]];
        break;
    }

    case 2: {
        cell.textLabel.text = FileCNames[indexPath.row];
        cell.detailTextLabel.text = FileCDetails[FileCNames[indexPath.row]];
        break;
    }

default: break;
}

return cell;

【讨论】:

  • @XCodeBeginner - 没关系,请将其标记为已回答
猜你喜欢
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多