Swift 自定义 UITableViewController:switch 语句 case 标签部分枚举
Swift custom UITableViewController: switch statement case label section enum
为了使我的代码更具可读性和可维护性,在带有类型 Int
的控制表达式的 switch 语句中使用标签而不是硬编码 Int
的 case 标签的最佳方法是什么?
例如,在我的 SettingsTableViewController
中,我试过
enum Section : Int {
case Phone
case LogOut
case DeleteAccount
}
并在 – tableView:didSelectRowAtIndexPath:
switch indexPath.section {
case .Phone:
// Push EditPhoneViewController
case .LogOut:
// Log out
case .DeleteAccount:
// Present action-sheet confirmation
}
但是遇到编译错误
Enum case pattern cannot match values of the non-enum type 'Int'
在开关中你不能简单地使用 indexPath.section
因为它与你的枚举类型不同。
使用switch Section(rawValue: indexPath.section)!
为了使我的代码更具可读性和可维护性,在带有类型 Int
的控制表达式的 switch 语句中使用标签而不是硬编码 Int
的 case 标签的最佳方法是什么?
例如,在我的 SettingsTableViewController
中,我试过
enum Section : Int {
case Phone
case LogOut
case DeleteAccount
}
并在 – tableView:didSelectRowAtIndexPath:
switch indexPath.section {
case .Phone:
// Push EditPhoneViewController
case .LogOut:
// Log out
case .DeleteAccount:
// Present action-sheet confirmation
}
但是遇到编译错误
Enum case pattern cannot match values of the non-enum type 'Int'
在开关中你不能简单地使用 indexPath.section
因为它与你的枚举类型不同。
使用switch Section(rawValue: indexPath.section)!