gpt4 book ai didi

macos - Swift 中的 EventKit 不适用于 Swift 2.0

转载 作者:行者123 更新时间:2023-11-30 14:07:38 24 4
gpt4 key购买 nike

我正在尝试迁移到 swift 2.0,但并非所有代码都能正常工作。我已经成功迁移了大部分代码,但在使用 EventKit 时仍然遇到问题。下面的代码在 swift 1.2 上运行良好。但现在我遇到了问题

import Foundation
import EventKit
import Cocoa

private let _SingletonSharedInstance = EventStore()

class EventStore {
let eventStore = EKEventStore ()

class var sharedInstance : EventStore {
return _SingletonSharedInstance
}

init() {
var sema = dispatch_semaphore_create(0)
var hasAccess = false

eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: { (granted:Bool, error:NSError?) in hasAccess = granted; dispatch_semaphore_signal(sema) })

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
if (!hasAccess) {
println("ACCESS", "ACCESS LONG")
let sharedWorkspace = NSWorkspace.sharedWorkspace()
sharedWorkspace.openFile("/Applications/System Preferences.app")
exit (0)
}
}
}

以下行导致了问题

eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: { (granted:Bool, error:NSError?) in hasAccess = granted; dispatch_semaphore_signal(sema) })

我收到错误

cannot invoke "requestAccessToEntityType" with an argument list of type '(EKEEntityType, completion: (Bool, NSError?) -> ())'

我读到使用 NSError! 应该可以解决问题,但事实并非如此。有任何想法吗?

最佳答案

我找到了答案。 bool 类型已更改为 ObjCBool​​,EKEntityTypeEvent 已更改为 EKEntityType.Event

import Foundation
import EventKit
import Cocoa

private let _SingletonSharedInstance = EventStore()

class EventStore {
let eventStore = EKEventStore ()

class var sharedInstance : EventStore {
return _SingletonSharedInstance
}

init() {
var sema = dispatch_semaphore_create(0)
var hasAccess:ObjCBool = false

eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted:ObjCBool, error:NSError?) in hasAccess = granted; dispatch_semaphore_signal(sema) })

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
if (!hasAccess) {
println("ACCESS", "ACCESS LONG")
let sharedWorkspace = NSWorkspace.sharedWorkspace()
sharedWorkspace.openFile("/Applications/System Preferences.app")
exit (0)
}
}
}

关于macos - Swift 中的 EventKit 不适用于 Swift 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com