Swift自定义UITableViewController:switch语句大小写标签部分枚举

ma1*_*w28 1 enums switch-statement uitableview ios swift

为了使我的代码更具可读性和可维护性,Int在带有类型为控件的switch语句中的案例标签中,使用标签代替硬编码的s 的最佳方法是什么Int

例如,在我的内部SettingsTableViewController,我尝试过

enum Section : Int {
    case Phone
    case LogOut
    case DeleteAccount
}
Run Code Online (Sandbox Code Playgroud)

和在 – tableView:didSelectRowAtIndexPath:

switch indexPath.section {
case .Phone:
    // Push EditPhoneViewController
case .LogOut:
    // Log out
case .DeleteAccount:
    // Present action-sheet confirmation
}
Run Code Online (Sandbox Code Playgroud)

但是得到了编译错误

Enum case pattern cannot match values of the non-enum type 'Int'
Run Code Online (Sandbox Code Playgroud)

pte*_*fil 5

在开关中,您不能简单地使用indexPath.section它,因为它与您的枚举类型不同。

使用 switch Section(rawValue: indexPath.section)!