我使用this教程创建了一个应用。但这是行不通的。 我写的一切都像在教程中一样。但是此代码不起作用。而且它不想转换为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
答案 0 :(得分:-2)
表达类型'(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")
}