我正在将以下代码转换为 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
或将 _code
与 Error
实例一起使用,而不是代码
。
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/