gpt4 book ai didi

ios - Swift 自定义 UITableViewController : switch statement case label section enum

转载 作者:搜寻专家 更新时间:2023-11-01 06:22:53 25 4
gpt4 key购买 nike

为了使我的代码更具可读性和可维护性,使用标签而不是硬编码的最佳方式是什么 Int s 用于带有 Int 类型控制表达式的 switch 语句中的 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)!

关于ios - Swift 自定义 UITableViewController : switch statement case label section enum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30221316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com