做声明

The do statement

假设这是我们从模型中删除对象的函数:

func delete(indexPath: IndexPath) {

    let managedObject = self.fetchedResultsController.object(at: indexPath)
    self.managedObjectContext.delete(managedObject)

    do {

        // Save changes
        try self.managedObjectContext.save()

        // Cancel the notification
        let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
        center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])

        // Reload tableView
        self.tableView.reloadData()

    } catch {

        let alertController = UIAlertController.init(title: "Error", message: "We are sorry! Unknown error occured...", preferredStyle: .alert)
        alertController.addAction(UIAlertAction.init(title: "Close", style: .default, handler: { (action) in
            // Completion handler
        }))
        self.present(alertController, animated: true, completion: {
            // Completion block
        })
    }
}

问题:

如果抛出错误,执行将转移到 catch 子句。那很好。但是语句(其余代码,取消通知和重新加载 tableView)会发生什么?这些语句仍然是可执行的还是被简单地忽略了?

如果你希望你的代码在抛出错误时仍然被调用,你可以为它创建一个单独的函数,然后从 docatch 的末尾调用它陈述。或者您可以将该代码放在 defer 语句中。

或者类似

func delete(indexPath: IndexPath) {

let managedObject = self.fetchedResultsController.object(at: indexPath)
self.managedObjectContext.delete(managedObject)

do {

    // Save changes
    try self.managedObjectContext.save()
    reloadCode()

} catch {
    reloadCode()
    let alertController = UIAlertController.init(title: "Error", message: "We are sorry! Unknown error occured...", preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "Close", style: .default, handler: { (action) in
        // Completion handler
    }))
    self.present(alertController, animated: true, completion: {
        // Completion block
    })
}
}

func reloadCode() {
        // Cancel the notification
        let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
        center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])

        // Reload tableView
        self.tableView.reloadData()

}

或者更巧妙地,

func delete(indexPath: IndexPath) {

let managedObject = self.fetchedResultsController.object(at: indexPath)
self.managedObjectContext.delete(managedObject)


  defer {
 let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
        center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])

        // Reload tableView
        self.tableView.reloadData()
}
do {

    // Save changes
    try self.managedObjectContext.save()

    // Cancel the notification


} catch {

    let alertController = UIAlertController.init(title: "Error", message: "We are sorry! Unknown error occured...", preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "Close", style: .default, handler: { (action) in
        // Completion handler
    }))
    self.present(alertController, animated: true, completion: {
        // Completion block
    })
  }
}