swift - CNContactStore 保存错误

标签 swift error-handling contacts swift2

CNContactStore 的 executeSaveRequest(_:) 方法根据文档抛出错误。

我试图在 do/catch 中捕获此错误,但我无法弄清楚我需要捕获什么错误。

do{
  try store.executeSaveRequest(saveRequest)
} catch *???* {
  //alert the user
}

应该用什么替换上面代码中的 ????

最佳答案

实际上你有几个选择。

在不知道错误的情况下捕获任何错误

捕捉{...}

  1. 使用特定的错误消息捕获任何错误

catch let error {//使用错误 }

  1. 使用 exhaustive catch clauses使用 CNErrorCode 枚举处理特定错误。

    enum CNErrorCode : Int {
    
        case CommunicationError
        case DataAccessError
    
        case AuthorizationDenied
    
        case RecordDoesNotExist
        case InsertedRecordAlreadyExists
        case ContainmentCycle
        case ContainmentScope
        case ParentRecordDoesNotExist
    
        case ValidationMultipleErrors
        case ValidationTypeMismatch
        case ValidationConfigurationError
    
        case PredicateInvalid
    
        case PolicyViolation
    }     
    

关于swift - CNContactStore 保存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31004506/

相关文章:

forms - 联系表格 - 成功消息

ios - UITapGestureRecognizer 不适用于 UICollectionView header

php - 在 Woocommerce 中更改错误通知文本并针对此错误采取措施

ios - 在 Swift 的 AppDelegate 中访问 UIViewCotroller

ios - AFNetworking错误代码= -1005 “The network connection was lost”

error-handling - reactjs尝试在渲染中捕获不捕获子级错误

android - 为什么我的短信在我的列表中没有正确显示?

android - android 联系人数据中的 TIMES_CONTACTED 值问题

ios - 发送到实例的 Swift UITableView 无法识别的选择器

ios - 使用 Swift 5.1 for iOS 中的 SwiftUI 使用尺寸完美适合的 RadialGradient 填充动态大小的矩形