[英]How to check enum in same value using switch in swift
對於這種情況,我有點障礙,我有一個具有相同值的枚舉,現在這里的問題是它們有不同的用case
,我怎樣才能在 switch 中設置一個條件:
假設我有這個:
enum ApiType: String, CaseIterable {
case dashboard = "dashboardRootIdApi"
case profile = "profileRootIdApi"
case usemeInLogin = "authenticationAccessIdApi"
case usemeInLogout = "authenticationAccessIdApi"
}
並從我的用例類中:
func authenticationDtoScreen(for: ApiType) -> UIControllerSharedScreen {
switch myType {
case .usemeInLogin: {
return UIControllerScreenConfiguration(
for: .usemeInLogin,
title: "Login"
)
}
case .usemeInLogout: {
return UIControllerScreenConfiguration(
for: .usemeInLogout,
title: "Logout"
)
}
}
}
我知道.usemeInLogout
永遠不會因為這個.usemeInLogin
而發生。
沒有上下文它有點難,但我建議使用簡單的枚舉來區分用例:
enum MyType: String {
case usemeInLogin
case usemeInLogout
}
如果需要,從這個枚舉到字符串有一個 map,比如:
var map: [MyType:String] = [:]
map[.usemeInLogin] = "something"
map[.usemeInLogout] = "something"
這些字符串值不必是枚舉的原始值。 它可以是一個計算屬性:
enum ApiType: CaseIterable {
case dashboard
case profile
case usemeInLogin
case usemeInLogout
var apiType: String {
switch self {
case .dashboard: return "dashboardRootIdApi"
case .profile: return "profileRootIdApi"
case .usemeInLogin, .usemeInLogout: return "authenticationAccessIdApi"
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.