简体   繁体   中英

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 .

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:

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. Surprisingly, I am receiving the error message

'DelegateProxy' requires that 'WebSocketClient' be a class type

I am not sure what I am missing here and would appreciate pointers!

Try with @objc like below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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