gpt4 book ai didi

swift - 如何使用类型为 "Cannot invoke ' 的参数列表修复 '(eventStore: EventStoring)' createEvent'

转载 作者:行者123 更新时间:2023-11-28 13:52:12 27 4
gpt4 key购买 nike

我正在使用 EventKit 的 EKEventStore,我想模拟它和 EKEvent。但是我不知道如何正确抽象EKEvent's init(eventStore: EKEventStore) 和其他方法。

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
}
}

此处的错误是:“'Self' 仅在协议(protocol)中可用或作为类中方法的结果可用;您是说 'EKEvent' 吗?”并且:“无法将类型 'EKEvent' 的返回表达式转换为返回类型'Self'”

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)
}
}

在倒数第七行,错误是:无法使用 '(eventStore: EventStoring)' 类型的参数列表调用 'createEvent'在倒数第三个是:无法使用 '(StoreEvent, span: EKSpan, commit: Bool)' 类型的参数列表调用 'save'

更新 由于我采纳了 Dan 的建议,在我的实现过程中出现了另一个同类问题,所以我更新了我的问题

最佳答案

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

首先,我按照 Dan 的建议更改了 GenericStoreManagerstore 属性的类型

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)
}

...

}

然后我更改了在 extension EKEvent: EventStoreEvent 中获取返回值的方式,因此它也可以在 EKEvent 的子类中工作

extension EKEvent: EventStoreEvent {
typealias MatchingEventStore = EKEventStore

static func createEvent(eventStore: MatchingEventStore) -> Self {
return self.init(eventStore: eventStore)
}
}

关于swift - 如何使用类型为 "Cannot invoke ' 的参数列表修复 '(eventStore: EventStoring)' createEvent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54406371/

27 4 0
文章推荐: swift - 如何在捕获 session 中将 CIFilter 与 CIPerspectiveCorrection 结合使用?
文章推荐: html - site.css 中的 CSS 不适用于 asp.net 布局中的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com