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

Va *_*sal 0 error-code ios touch-id swift swift3

我正在将下面的代码转换为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) {
Run Code Online (Sandbox Code Playgroud)

步骤4在Xcode 8,Swift 3上不再起作用.所以我不能做以下情况:

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

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

非常感谢!

Nir*_*v D 9

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

reply: { (success : Bool, error : Error? ) -> Void in
Run Code Online (Sandbox Code Playgroud)

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

performSegue(withIdentifier: "dismissLogin", sender: self)
Run Code Online (Sandbox Code Playgroud)

在斯威夫特3,你需要转换Error对象NSError,或者使用_codeError实例,而不是code.

switch((error! as NSError).code)
Run Code Online (Sandbox Code Playgroud)

要么

switch(error!._code)
Run Code Online (Sandbox Code Playgroud)

您还需要像这样更改调度语法.

Dispatch.async.main {
    //write code
}
Run Code Online (Sandbox Code Playgroud)