Search code examples
iosswiftxcodeswift4

How to check enum in same value using switch in swift


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.


Solution

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