表达式类型"(布尔值,错误?)-> Void"不明确,没有更多上下文



我使用本教程创建了一个应用程序。但它不起作用。我写的都和教程里的一样。但是这个代码不起作用。而且它不想转换成Swift 4。这是我的代码:

import UIKit
import LocalAuthentication
class ViewController: UIViewController {
func showAlertController(_ message: String) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
}
@IBAction func touchID(_ sender: Any) {
let context = LAContext()
var error: NSError?

if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "Authenticate with Touch ID"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply:
{(succes, error) in
if succes {
self.showAlertController("Touch ID Authentication Succeeded")
}
else {
self.showAlertController("Touch ID Authentication Failed")
}
} as! (Bool, Error?) -> Void)
}
else {
showAlertController("Touch ID not available")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

这是一个错误:

38:23 Expression type '(Bool, Error?) -> Void' is ambiguous without more context

38:23 Cast from '(_, _) -> ()' to unrelated type '(Bool, Error?) -> Void' always fails

表达式类型'(Bool,Error?(->Void'在没有更多上下文的情况下是不明确的,因为总是失败,所以只需删除:

as! (Bool, Error?) -> Void

以下是额外的:

if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "Authenticate with Touch ID"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply:
{(succes, error) in
if succes {
self.showAlertController("Touch ID Authentication Succeeded")
}
else {
self.showAlertController("Touch ID Authentication Failed")
}
} )
}
else {
showAlertController("Touch ID not available")
}

最新更新