How do I implement a Swift protocol with a generic constrained type property?

只愿长相守 提交于 2019-12-05 17:33:35

Personally, I feel like some of your code just doesn't really make sense. Like :

extension ShimEndPoint: ReturnType {
    var returnType: ImmutableMappable.Type {
        switch self {
        case .deAuthorize(_, _):
            return EmptyResponse.self
        case .authorize(_, _):
            return AuthorizeResponse.self
        case .step(_, _, _, _):
            return StepResponse.self 
        }
    }
}

How can an object determined the type of itself based on itself ? And then return a value type different than itself ?

So what I'm trying to help here is just trying to find a solution of what you are trying to achieve, rather than solve the existing code.

I assume that what you trying to do is determined a type of object to map to depends on the result case at runtime. The enum itself is ImmutableMappable and it has 3 cases : .authorize, .deauthorize and .step.

The function .request will return this type and determined the type to map to, accordingly to the case. However, since the type of object need to be determined at compile time, all the result type EmptyResponse, AuthorizeResponse and StepResponse here must be following a same protocol ( I assume, Response here) and the result will also only return the protocol rather a specific class type.

return Shim.provider
    .request(endPoint)
    .timeout(7,
             scheduler: MainScheduler.asyncInstance)
    .retry(3)
    .flatMap { result: ImmutableMappable -> Observable<Response> in
       return Observable.create{
        switch result {
          case .authorize:
            observer.onNext(AuthorizeResponse())
          case .deAuthorize:
            //etc
          case step:
            //etc.
           }
        }
     }

Hope this answer your question !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!