【问题标题】:CNContactStore Save ErrorCNContactStore 保存错误
【发布时间】:2015-06-23 13:44:38
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: swift error-handling contacts swift2


    【解决方案1】:

    实际上,您有几个选择。

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

    catch {...}

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

    catch let error { // Use 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
      }     
      

    【讨论】:

    • 谢谢!但是,您在哪里碰巧找到了 CNErrorCodes?我翻遍了整个文档,找不到它们。
    • 我也找不到它们。我只是在 Xcode 中输入 CNErrorCode 并命令点击它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多