2015-10-19 50 views
0

我在我的應用程序中使用下面的代碼來創建日曆。當我更新Xcode和移動到Swift 2.0時,此行let calendarWasSaved = eventStore.saveCalendar(newCalendar, commit: true, error: &error)上出現錯誤。Swift 2.0創建日曆拋出

// Save the calendar using the Event Store instance 
    var error: NSError? = nil 
    let calendarWasSaved = eventStore.saveCalendar(newCalendar, commit: true, error: &error) 


    // Handle situation if the calendar could not be saved 
    if calendarWasSaved == false { 
     let alert = UIAlertController(title: "Calendar could not save", message: error?.localizedDescription, preferredStyle: .Alert) 
     let OKAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
     alert.addAction(OKAction) 

     self.presentViewController(alert, animated: true, completion: nil) 
    } else { 
     insertEvent(eventStore) 
     NSUserDefaults.standardUserDefaults().setObject(newCalendar.calendarIdentifier, forKey: "EventTrackerPrimaryCalendar") 
    } 

的錯誤是Extra argument 'error' in call

當我改寫線它說,它「拋出」結尾。這是Swift 2.0的上述代碼版本嗎?

謝謝

回答

2

在處理swift 2.0中的錯誤時有兩種選擇。您可以抓住並處理每個案例,或者在拋出錯誤時忽略該語句。這裏是如何做到既

-ignore如果拋出

let calendarWasSaved = try? eventStore.saveCalendar(newCalendar, commit: true) // use iff calendarWasSAved doesn't affect the rest of the code (if maybe you can give it default value) 

- 把手錯誤

do { 
    let calendarWasSaved = try eventStore.saveCalendar(newCalendar, commit: true) 
} catch (Your error type) { 
    //error handling 
} catch (_){}// Other errors that you dot care about will fall here. not necessary if you handled each case 
+0

優秀。謝謝! –