Home iOS Development ios – Swift Enum Values With Generics

ios – Swift Enum Values With Generics

0

[ad_1]

I wanted to make a parent view model that contains some basic stuff that I want all my view models to have in SwiftUI. I’m trying to make the parent view model have a generic variables so I can inherit ParentViewModel in any view model I make with the custom type.

This is what I’ve tried and got a Cannot find type 'T' in scope error on the loaded case. Not really sure how I can make that generic, any insight here?

class ParentViewModel: ObservableObject {
    enum Status {
        case loading
        case loaded(T)
        case error(Error)
    }
    
    @Published var status: Status = .loading
}

class ChildViewModel: ParentViewModel {
    
    init() {
        self.status = .loading
    }
    
    init(object: SomeObject) {
        self.status = .loaded(object)
    }
}

[ad_2]