如何使用通用约束类型属性实现Swift协议? [英] How do I implement a Swift protocol with a generic constrained type property?

查看:149
本文介绍了如何使用通用约束类型属性实现Swift协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个看起来像这样的协议:

I would like to have a protocol that looks something like this:

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

实现协议的枚举部分:

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和StepResponse都实现ImmutableMappable. 现在,我想在函数调用中使用"returnType"属性:

EmptyResponse, AuthorizeResponse and StepResponse all implement ImmutableMappable. Now I would like to use the "returnType" property in a function call:

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

mapObject行给了我以下编译器错误: 无法将类型'ImmutableMappable.Type'的值转换为预期的参数类型'T.Type'

The line mapObject gives me the following compiler error: "Cannot convert value of type 'ImmutableMappable.Type' to expected argument type 'T.Type'

"mapObject"的功能签名为:

The function signature of "mapObject" is:

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

如何定义和实现协议,以便可以将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.

我假设您要尝试执行的操作是确定要映射的对象类型,具体取决于运行时的结果情况.枚举本身是ImmutableMappable,它有3种情况:.authorize.deauthorize.step.

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.

函数.request将返回这种类型,并根据情况确定要映射的类型.但是,由于需要在编译时确定对象的类型,因此此处的所有结果类型EmptyResponseAuthorizeResponseStepResponse必须遵循相同的协议(我假设此处为Response),结果将为也只返回协议,而不是特定的类类型.

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 !

这篇关于如何使用通用约束类型属性实现Swift协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆