-1

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.

3
  • 3
    You need to change the raw value (string) of your enum cases so they are unique, that’s required for an enum. Commented Sep 12, 2022 at 7:37
  • 2
    Yep. Won't work as long as they have the same value. Redesign the code to separate between use-case differentiation and the string value you are using. Commented Sep 12, 2022 at 7:41
  • @DimaG. Thanks for the response! is there anyway I could use them on any usecases each scenario? 2 features are using them and have it's own uniqueness for them. Commented Sep 12, 2022 at 8:10

2 Answers 2

2

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"
   }
 }
}
Sign up to request clarification or add additional context in comments.

3 Comments

so basically whenever I use this cases of enum then apiType will fire off to initiate it's value?
yes, you'd do myType.apiType whenever you wanted that specific string value, but login and logout would be distinct enum cases
this is a good idea actually, but let's just say we have 200 of api types already and it uses in different use-cases on their side, and actually trying to refactoring them without any knowledge from there feature will break the architecture or logic :(
0

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"

4 Comments

I added a scenario for this case mate. hope it will help to resolve this issue.
I see you've added some code, but I still don't see in your example why do you insist on keeping explicit string values for each Enum value and having two of them the same value.
this is actually from the backend implementation, the apiType is we use in feature once it requires an secure action screen before continuing but in this case 2 features will actually sharing one apiType the difference is that the other feature doesn't have same screen implementation as we need to provide, and this shared component secure screen have the implementation already of switch statement of every cases in apiType.
Well, I still believe you need to correctly re-define the problem you are solving and then you'll find a solution. Maybe it's adding one more parameter to authenticationDtoScreen(...) and UIControllerScreenConfiguration() or something else. Don't try to abuse Enum's purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.