如何解决“无法使用类型为'(eventStore:EventStoring)'的参数列表调用'createEvent''”

时间:2019-01-28 16:30:40

标签: swift generics initialization protocols eventkit

我正在使用<key name="InstallationFolder"> <string> <value><![CDATA[my value]]></value></string> </key> EventKit's,我想模拟它以及EKEvent。 但是我不知道如何正确抽象EKEventStore EKEvent's和其他方法。

init(eventStore: EKEventStore)

这里的错误是:“'Self'仅在协议中可用,或者作为类中方法的结果可用;您是说protocol EventStoring { associated type Event: EventStoreEvent where Event.MatchingEventStore == Self func save(_ event: Event, span: EKSpan, commit: Bool) throws // Other methods of EKEventStore I use } extension EKEventStore: EventStoring { typealias Event = EKEvent } protocol EventStoreEvent { associatedtype MatchingEventStore: EventStoring static func createEvent(eventStore: MatchingEventStore) -> Self } extension EKEvent: EventStoreEvent { typealias MatchingEventStore = EKEventStore static func createEvent(eventStore: MatchingEventStore) -> Self { return EKEvent(eventStore: eventStore) as! Self } } 吗?” 和:“无法将类型'EKEvent'的返回表达式转换为类型'Self'”

'EKEvent'

最后第七行,错误是:无法调用类型为class GenericEventManger<StoreEvent: EventStoreEvent> { var store: EventStoring required init(with eventStore: EventStoring) { self.store = eventStore } func createEvent() -> StoreEvent { let eventStoreEvent: EventStoreEvent = StoreEvent.createEvent(eventStore: store) // Then some code where I configure the event... try store.save(eventStoreEvent, span: .thisEvent, commit: true) } } 的参数列表的'createEvent' 第三,它是:无法调用类型为'(eventStore: EventStoring)'

的参数列表的“保存”

更新由于我采用了Dan的建议,因此在实现过程中出现了同样的问题,因此我更新了问题

1 个答案:

答案 0 :(得分:1)

我想在Dan的帮助下,到目前为止,我已经找到了两个问题的解决方案,但是我还没有对它进行彻底的测试:

首先,我像Dan推荐的那样更改了store的{​​{1}}属性的类型

GenericStoreManager

然后我更改了在class GenericStoreManger<StoreEvent: EventStoreEvent> { var store: StoreEvent.MatchingEventStore func createEvent() -> StoreEvent { let eventStoreEvent: EventStoreEvent = StoreEvent.createEvent(eventStore: store) // Then some code where I configure the event... try store.save(eventStoreEvent as! StoreEvent.MatchingEventStore.Event, span: .thisEvent, commit: true) } ... } 中获取返回值的方式,因此它也可以使用EKEvent的子类

extension EKEvent: EventStoreEvent