Home > Blockchain >  How to check enum in same value using switch in swift
How to check enum in same value using switch in swift

Time:09-12

I'm kinda block for this scenario , I have a enum which have the same value now the question here is that they have different usecases how can I put a condition for this to case in switch:

supposed I have this:

enum ApiType: String, CaseIterable {
 case dashboard = "dashboardRootIdApi"
 case profile = "profileRootIdApi"
 case usemeInLogin = "authenticationAccessIdApi"
 case usemeInLogout = "authenticationAccessIdApi"
}

and from my usecases classes:

func authenticationDtoScreen(for: ApiType) -> UIControllerSharedScreen {
 switch myType {
 case .usemeInLogin: {
  return UIControllerScreenConfiguration(
   for: .usemeInLogin,
   title: "Login"
  )
 }
 case .usemeInLogout: {
  return UIControllerScreenConfiguration(
   for: .usemeInLogout,
   title: "Logout"
  )
 }
 }
}

I know .usemeInLogout will never be happend cause of this .usemeInLogin.

CodePudding user response:

Its a bit hard without context, but I'd suggest using simple enum for use case differentiation:

enum MyType: String {
 case usemeInLogin
 case usemeInLogout
}

And if needed, have a map from this enum to String, like:

var map: [MyType:String] = [:]
map[.usemeInLogin] = "something"
map[.usemeInLogout] = "something"

CodePudding user response:

Those string values don't have to be the raw value of your enum. It can be a calulated property:

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"
   }
 }
}
  • Related