工作但是在更新到swift 3和swift 4之后现在有错误;尝试恢复但仍然有错误
let eventStore = EKEventStore();
let newCalendar = EKCalendar(for: .event, eventStore: eventStore)
newCalendar.title = newPatientCal.text ?? "Add New Calendar"
let sourcesInEventStore = eventStore.sources
newCalendar.source = sourcesInEventStore.filter{
(source: EKSource) -> Bool in
source.sourceType.rawValue == EKSourceType.local.rawValue
}.first! // <---- Error occurs here; removed "!" but still have error
do {
try eventStore.saveCalendar(newCalendar, commit: true)
UserDefaults.standard.set(newCalendar.calendarIdentifier, forKey: "PatientTrackingCalendar")
newPatDelegate.calDidAdd()
self.dismiss(animated: true, completion: nil)
} catch {
let alert = UIAlertController(title: "Could not save new Calendar", message: (error as NSError).localizedDescription, preferredStyle: .alert)
let oKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(oKAction)
self.present(alert, animated: true, completion: nil)
}
答案 0 :(得分:0)
source
的{{1}}属性不接受可选项。但是你永远不应强制解包一个可选项,因为如果可选项是EKCalendar
,它会让你的应用程序崩溃。
我会更改您将nil
设置为以下内容的代码:
source