线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x10039fb00)

时间:2017-09-08 00:47:34

标签: ios swift xcode

工作但是在更新到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)
    }

1 个答案:

答案 0 :(得分:0)

source的{​​{1}}属性不接受可选项。但是你永远不应强制解包一个可选项,因为如果可选项是EKCalendar,它会让你的应用程序崩溃。

我会更改您将nil设置为以下内容的代码:

source