首页 > 解决方案 > 参数类型“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
    }
}

标签: swift

解决方案


您需要将实际值传递给初始化程序

self.init(store: store)

推荐阅读