简体   繁体   English

如何使用通用约束类型属性实现Swift协议?

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

I would like to have a protocol that looks something like this: 我想有一个看起来像这样的协议:

protocol ReturnType {
    var returnType: ImmutableMappable.Type { get }
}

The part of the enum implementing the protocol: 实现协议的枚举部分:

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

EmptyResponse, AuthorizeResponse and StepResponse all implement ImmutableMappable. EmptyResponse,AuthorizeResponse和StepResponse都实现了ImmutableMappable。 Now I would like to use the "returnType" property in a function call: 现在我想在函数调用中使用“returnType”属性:

return Shim.provider
    .request(endPoint)
    .timeout(7,
             scheduler: MainScheduler.asyncInstance)
    .retry(3)
    .mapObject(endPoint.returnType)

The line mapObject gives me the following compiler error: "Cannot convert value of type 'ImmutableMappable.Type' to expected argument type 'T.Type' 行mapObject给出了以下编译器错误:“无法将类型'ImmutableMappable.Type'的值转换为期望的参数类型'T.Type'

The function signature of "mapObject" is: “mapObject”的函数签名是:

public func mapObject<T : ImmutableMappable>(_ type: T.Type) -> RxSwift.Observable<T>

How do I define and implement the protocol so I can pass in my returnType to the mapObject function? 如何定义和实现协议,以便将returnType传递给mapObject函数?

I found a similar question but unfortunately I could not solve my problem with the help of the answer given: Returning constrained generics from functions and methods 我发现了一个类似的问题,但不幸的是我无法在给出答案的帮助下解决我的问题: 从函数和方法中返回约束泛型

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 . 枚举本身是ImmutableMappable ,它有三种情况: .authorize.deauthorize.step

The function .request will return this type and determined the type to map to, accordingly to the case. 函数.request将返回此类型,并根据情况确定要映射到的类型。 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. 但是,由于对象的类型需要在编译时确定, StepResponse这里的所有结果类型EmptyResponseAuthorizeResponseStepResponse必须遵循相同的协议(我假设, Response在这里),结果也只返回协议而不是特定的班级类型。

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 ! 希望这能回答你的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM