1

我对这段代码有疑问

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

编译器无法编译,错误是“使用未解析的标识符'EKSpanThisEvent'”

我希望有人能帮帮忙!

4

3 回答 3

5

在 Swift 2 / Xcode 7 中,为了保持一致性,很多枚举已经改变

try eventStore.saveEvent(event, span: .ThisEvent)
于 2015-08-08T15:22:24.963 回答
1

如果您处理以下引发的错误会更好saveEvent

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

更多关于 Swift 中的错误处理:https ://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

于 2016-01-13T12:10:40.547 回答
0

对于斯威夫特 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")
}
于 2016-10-28T02:02:29.497 回答