Home > Net >  Multiple Enums with same case
Multiple Enums with same case

Time:04-04

Is there a better approach to tackle this situation.

I am following MVVM for my project and we have ViewModels for every ViewController, which feed them the required data. The data is passed back through enums with associatedValues.

Suppose, we have

enum HomeViewModelState {
    case failure(String)
    case anotherState(SomeModel)
}

enum ListingViewModelState {
    case failure(String)
    case anotherState(SomeModel)
}

As we see we have a repetitive case i.e. failure(String) that pass back the error message incase of an api failure. I want to avoid this repetitive task. Is there a better approach ?

CodePudding user response:

You could create one state enum that uses generics:

enum ViewModelState<T> {
    case failure(String)
    case anotherState(T)
}
  • Related