Swift 中的 EventKit 不适用于 Swift 2.0
EventKit in Swift not working with Swift 2.0
我正在尝试迁移到 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)
}
}
}
我正在尝试迁移到 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)
}
}
}