简体   繁体   中英

Swift custom UITableViewController: switch statement case label section enum

To make my code more readable & maintainable, what's the best way to use labels instead of hardcoded Int s for case labels in a switch statement with a control expression of type Int ?

Eg, inside my SettingsTableViewController , I tried

enum Section : Int {
    case Phone
    case LogOut
    case DeleteAccount
}

and in – tableView:didSelectRowAtIndexPath:

switch indexPath.section {
case .Phone:
    // Push EditPhoneViewController
case .LogOut:
    // Log out
case .DeleteAccount:
    // Present action-sheet confirmation
}

but got the compile error

Enum case pattern cannot match values of the non-enum type 'Int'

In the switch you can't simply use indexPath.section as that is not the same type as your enum.

Use switch Section(rawValue: indexPath.section)!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM