我目前正在Xcode 11.4中学习Swift 5。我试图构建一个代码,当一个按钮被按下时,它使用face-id来验证用户。一旦用户被认证,它就会切换到不同的视图控制器。
目前face-id可以工作,但之后应用程序就会崩溃。我在目前的行上收到这个错误。
在闭包中隐式使用'self';使用'self.'使捕获语义显式化。
@IBAction func buttonTapped(_ sender: Any) {
let vc = storyboard?.instantiateViewController(identifier: "blue_vc") as! blueViewController
let context:LAContext = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil){
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "message") { (good, error) in
if good {
print("good")
present(vc,animated: true)
} else {
print("error")
}
}
}
}
任何帮助将是巨大的!
添加 self
来精确地引用现有的视图控制器,就像这样。
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "message") { (good, error) in
if good {
print("good")
self.present(vc, animated: true)
}
else {
print("error")
}
}
因为evaluatePolicy completionhandler是一个在函数返回后执行的转义闭包,你需要提供self来进行显式捕获。
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "message") { (success, error) in
guard success else { return }
let vc = storyboard?.instantiateViewController(identifier: "blue_vc") as! blueViewController
self.present(vc,animated: true)
}