swift - 参数类型“ProtocolName.Protocol”不符合预期类型“ProtocolName”`
问题描述
当我有一个便利初始化程序时,我得到一个编译器错误,但是当我删除它并有一个指定的初始化程序时,编译器不会抱怨。这是怎么回事?
我确实将协议的具体实现传递给构造函数。
编译器问题:
编译器错误:File.swift:16:20: Argument type 'IngressEventStore.Protocol' does not conform to expected type 'IngressEventStore'
public protocol IngressEventStore {
func doSomething()
}
public class AddEventsToStoreOperation: Operation {
private let store: IngressEventStore
var events: [IngressEvent]?
init(store: IngressEventStore) {
self.store = store
}
convenience init(store: IngressEventStore, events: [IngressEvent]) {
// !! This line is where it complains
self.init(store: IngressEventStore)
self.events = events
}
}
没有编译器问题:
public protocol IngressEventStore {
func doSomething()
}
public class AddEventsToStoreOperation: Operation {
private let store: IngressEventStore
var events: [IngressEvent]?
init(store: IngressEventStore, events: [IngressEvent]) {
self.store = store
self.events = events
}
}
解决方案
您需要将实际值传递给初始化程序
self.init(store: store)
推荐阅读
- amazon-web-services - 如何使用 boto3 删除 s3 生命周期规则
- sql - SQL Server - 在多天内按分钟聚合数据
- c++ - 如何将基于 SQLite3 C 的 SELECT 查询的结果检索为 JSON?
- c++ - 如何将二进制文件转换为 size_t?
- php - PHP 中同一个 shuffle 中的两个不同 JSON 键
- node.js - 使用 Homebrew 在一台机器上安装 Node v8 和 v14
- python - 重复但在 numpy 中以可变大小的块
- python - 使用 python 抓取网页
- class - ES6 中的单例类
- karate - 空手道 - 数据外部化