ios - canEvaluatePolicy 函数中的 "switch(error!.code)"在 Swift 3 (Xcode 8) 中不存在

标签 ios swift swift3 error-code touch-id

我正在将以下代码转换为 Swift 3。

 if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error:nil) {

  // 2.
  context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics,
    localizedReason: "Logging in with Touch ID",
    reply: { (success : Bool, error : NSError? ) -> Void in

      // 3.
      dispatch_async(dispatch_get_main_queue(), {
        if success {
          self.performSegueWithIdentifier("dismissLogin", sender: self)
        }

        if error != nil {

          var message : NSString
          var showAlert : Bool

          // 4.
          switch(error!.code) {

第 4 步在 Xcode 8、Swift 3 上不再有效。所以我无法执行以下情况:

switch(error!.code) {
          case LAError.AuthenticationFailed.rawValue:
            message = "There was a problem verifying your identity."
            showAlert = true
            break;

目前,我似乎还没有找到解决方案。有任何建议,请告诉我。

非常感谢!

最佳答案

首先更改evaluatePolicy 方法的回复闭包,在Swift 3 中它是Error 而不是NSError

reply: { (success : Bool, error : Error? ) -> Void in

其次,使用这样的标识符更改 performSegue。

performSegue(withIdentifier: "dismissLogin", sender: self)

在 Swift 3 中,您需要将 Error 对象转换为 NSError 或将 _codeError 实例一起使用,而不是代码

switch((error! as NSError).code)

switch(error!._code)

您还需要像这样更改分派(dispatch)语法。

Dispatch.async.main {
    //write code
}

关于ios - canEvaluatePolicy 函数中的 "switch(error!.code)"在 Swift 3 (Xcode 8) 中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39743461/

相关文章:

ios - 为什么我的 iOS 应用程序在我从 iOS 模拟器打开时失败/关闭?

ios - AlamofireObjectMapper 在 iOS 8 中不工作

ios - 如何在按钮函数中传递特定函数的常量值以将其用作导航目的地

ios - 苹果自制iOS教程帮助: Unintended overlap with nested Stack Views

ios - 如何在 iTunes Connect 下启用 iOS Adob​​e AIR 应用程序进行 TestFlight 测试?

swift - 可以像这样制作 UITextField 吗?

ios - 基于 Storyboard的登录屏幕示例

ios - 后台模式下的多点连接

ios - 为什么 Microsoft Azure(或一般的 Swift)无法更新变量以在表查询后返回?

iOS 推送通知 : App as Provider?