Xcode7, iOS9 use of unresolved identifier 'EKSpanThisEvent'

2.7k Views Asked by At

i got a problem with this code

import UIKit
import EventKit

let eventStore = EKEventStore()
    eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (success, accessError) -> Void in
        if accessError != nil {
            print("Calendar Access Error: \(accessError)")
            return
        }
        let event = EKEvent(eventStore: eventStore)

        event.title = self.detailItem!.title
        event.allDay = true
        event.startDate = self.detailItem!.date
        event.endDate = self.detailItem!.date
        event.calendar = eventStore.defaultCalendarForNewEvents
        var saveToCalendarError: NSError?
        let success: Bool
        do {
            //Swift2: use of unresolved identifier 'EKSpanThisEvent'
            try eventStore.saveEvent(event, span: EKSpanThisEvent)
            success = true
        } catch var error as NSError {
            saveToCalendarError = error
            success = false
        } catch {
            fatalError()
        }

the compiler does not compile, and the error is "use of unresolved identifier 'EKSpanThisEvent'"

i hope someone can help!

3

There are 3 best solutions below

0
On BEST ANSWER

In Swift 2 / Xcode 7 a lot of enumerations have been changed for consistency

try eventStore.saveEvent(event, span: .ThisEvent)
0
On

For Swift 3

do {
   try self.eventStore.save(event, span: .thisEvent)
} catch let specError as NSError {
   print("A specific error occurred: \(specError)")
} catch {
   print("An error occurred")
}
0
On

Better if you handle the errors thrown by saveEvent:

do {
    try store.saveEvent(event, span: .ThisEvent)
} catch let specError as NSError {
    print("A specific error occurred: \(specError)")
} catch {
    print("An error occurred")
}

More about Error Handling in Swift: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html