我想使用返回整数的pageControl.currentPage来跟踪我的页面。我的switch语句设置如下:
let currentPage = pageControl.currentPage
switch currentPage {
case 0:
// execute code for first page
case 1:
// execute code for second page
case 2:
// execute code for third page
default: break
}
代替案例" 0"," 1"," 2",我希望更具语义性,例如
case FirstPage:
case SecondPage:
case ThirdPage:
我将如何做到这一点?
答案 0 :(得分:3)
您最好的选择是使用Int
值支持枚举。
你可以这样声明你的枚举:
enum PageEnum: Int {
case firstPage = 0 // Implicitly 0 if you don't set value for first enum.
case secondPage = 1 // Each enum after will automatically increase by 1
case thirdPage = 2 // so explicitly listing raw value is not necessary.
}
然后,您可以使用开关来确定页面值,如下所示:
switch PageEnum(rawValue: currentPage)! {
case .firstPage:
print("You're on the first page")
case .secondPage:
print("You're on the second page")
case .thirdPage:
print("You're on the third page")
default:
assert(false, "You shouldn't ever land here")
}