Я хочу использовать универсальные типы в классе с помощью быстрых протоколов:
public protocol WebSocketType {
// some interfaces
}
class _Network<WebSocketT: WebSocketType, Configuration: NetworkConfigurationType> {
// Use the generic type
let websocket: WebSocketT
init(host: String, api: String) {
// do something here...
// create the instance which will conform the protocol via generic type
// compilation error: 'WebSocketT' cannot be constructed because it has no accessible initializers
self.websocket = WebSocketT()
}
}
// I'll create a class with concrete classes (WebSocket and NetworkConfiguration).
// I don't have the class `WebSocket` and I'll extend that to conform the protocol `WebSocketType`.
extension WebSocket: WebSocketType {}
typealias Network = _Network<WebSocket, NetworkConfiguration>
let network = Network()
Я получил ошибку 'WebSocketT' cannot be constructed because it has no accessible initializers
и добавил init()
в протокол WebSocketType
:
public protocol WebSocketType {
init()
}
Тогда я получил еще одну ошибку Initializer requirement 'init()' can only be satisfied by a
требуется initializer in non-final class 'WebSocket'
.
Как я могу исправить эту проблему, чтобы создать универсальный тип в классе?