简体   繁体   English

Swift类型检查器拒绝将具有类要求的协议作为类类型

[英]Swift type checker rejects protocol that has a class requirement as a class type

This is about an error encountered in https://github.com/RxSwiftCommunity/RxStarscream/pull/23 . 这是关于https://github.com/RxSwiftCommunity/RxStarscream/pull/23中遇到的错误。

I am trying to generalize a type, so I can mock the class it is based on. 我试图概括一个类型,所以我可以模拟它所基于的类。 This means I want to switch from a concrete implementation to a protocol. 这意味着我想从具体的实现转换为协议。

The original class looks like this: 原始类如下所示:

open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelegate { ... }
    ...
}

I want to replace this with this protocol, that WebSocket is already extending: 我想用此协议替换它,该WebSocket已经扩展:

public protocol WebSocketClient : AnyObject {
    ...
}

This means I am replacing 这意味着我要更换

public class RxWebSocketDelegateProxy: DelegateProxy<WebSocket, NSObjectProtocol>, ... {
    ...
}

with

public class RxWebSocketDelegateProxy: DelegateProxy<WebSocketClient, NSObjectProtocol>, ... {
    ...
}

All of this seems easy enough — WebSocketClient provides all the functionality that is needed here. 所有这一切似乎很容易— WebSocketClient提供了此处所需的所有功能。 Surprisingly, I am receiving the error message 令人惊讶的是,我收到错误消息

'DelegateProxy' requires that 'WebSocketClient' be a class type 'DelegateProxy'要求'WebSocketClient'为类类型

I am not sure what I am missing here and would appreciate pointers! 我不确定我在这里缺少什么,不胜感激指针!

Try with @objc like below: 尝试使用@objc,如下所示:

@objc public protocol WebSocketClient : AnyObject {
    ---
}

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

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