Search code examples
macosswifteventkit

EventKit in Swift not working with Swift 2.0


I am trying to migrate to swift 2.0 but not all of my code is working. I have succed to migrate most of the code but I stillt have troubles using the EventKit. The code below was working fine with swift 1.2. But now I have a Problem

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

Following line is causing the Problems

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

I am getting the error

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

I read that using NSError! should solve the issue but it did not. Any ideas?


Solution

  • I found the answer. The bool type had changed to ObjCBool and EKEntityTypeEvent to 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)
            }
        }
    }