2017-09-08 11 views
-2

swift 3とswift 4に更新した後にエラーが発生しました。エラースレッド1:EXC_BREAKPOINT(コード= 1、サブコード= 0x10039fb00)

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

は安全にoptionalsのラップを解除することを学びます。 – rmaddy

+0

Swift 3または4にアップデートしようとしていますか? – Sneha

答えて

0

EKCalendarsource性質を持つように、リストアが、継続しようとする試みは、オプションを受け付けません。しかし、もしオプションがnilならあなたのアプリをクラッシュさせるので、オプションを強制的にアンラップするべきではありません。

私はこのような何かにsourceを設定するためのコードを変更します

if let aSource = sourcesInEventStore.filter { (source) -> Bool in 
    source.sourceType == .local 
    }.first { 
    newCalendar.source = aSource 
} else { 
    // optionally set newCalendar.source to some desired default 
} 
関連する問題