Swift 2.0 创建日历抛出

Swift 2.0 Create calendar throws

我在我的应用程序中使用下面的代码来创建日历。当我更新 Xcode 并移动到 Swift 2.0 时,此行 let calendarWasSaved = eventStore.saveCalendar(newCalendar, commit: true, error: &error).

现在出现错误
// 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

当我重写该行时,它说最后是 'throws'。这是上面代码的 Swift 2.0 版本吗?

谢谢

在 swift 2.0 中处理错误时有两种选择。您可以捕获它们并处理每种情况,或者如果它抛出错误则忽略该语句。以下是两者的方法

-如果抛出则忽略

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