mardi 15 décembre 2015

Resulving a class of and object that conforms to protocol

I would like to pass a object that confurms to some protocol, and resulve his type for allocation with Swinject framwork (By dependency injecyion).

protocol IViewModelAware {
    typealias T
    var viewModel : T { get set }
}


class ViewAndViewModelCreator {
    var container : Container

    init(container : Container) {
        self.container = container
    }

    func resulveViewModel<T : IViewModelAware>(controller : T) {
        let mirror = Mirror(reflecting: controller.viewModel)
        let viewModelClassType = mirror.subjectType
        let viewModel = self.container.resolve(viewModelClassType.self) // This line shows error
        controller.viewModel = viewModel
    }

}

Error: Cannot invoke 'resolve' with an argument list of type '(Any.Type)'

How do I get the Class from an object that confirms to protocol, maybe there is another option except reflection?





Aucun commentaire:

Enregistrer un commentaire