我一直想写一个嵌套函数,它接受touchID的原因字符串和bool值(如果它应该显示或不显示)。这是我的代码
import UIKit
import LocalAuthentication
class XYZ : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
presentTouchID(reasonToDsiplay: "Are you the owner?", true) //ERROR: Expression resolves to an unused function
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func presentTouchID(reasonToDsiplay reason: String, _ shouldShow: Bool) -> (Bool) -> (){
let reason1 = reason
let show = shouldShow
let long1 = { (shoudlShow: Bool) -> () in
if show{
let car = LAContext()
let reason = reason1
guard car.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil) else {return}
car.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {(success, error) in
guard error != nil else {return}
dispatch_async(dispatch_get_main_queue(), { Void in
print("Kwaatle")
})
}
}
else{
print("Mah")
}
}
return long1
}
}
当我presentTouchID(reasonToDsiplay: "Are you the owner?", true)
进入时
func viewDidLoad()
我收到错误
表达式解析为未使用的函数。
我做错了什么?
答案 0 :(得分:1)
问题是你的方法presentTouchID
返回一个闭包/函数。您调用presentTouchID
但不以任何方式使用返回的闭包。
你有几个选择。
1.调用返回的闭包:
presentTouchID(reasonToDsiplay: "Are you the owner?", true)(true)
看起来真的很尴尬 2.您可以将返回的闭包存储在变量中:
let present = presentTouchID(reasonToDsiplay: "Are you the owner?", true)
我不确定这里是否有任何意义。
3.您可以从presentTouchID
中删除布尔值作为参数
4. OR 修复返回的闭包
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
presentTouchID(reasonToDsiplay: "Are you the owner?", true) { success in
if success {
print("Kwaatle")
} else {
print("Mah")
}
}
}
func presentTouchID(reasonToDsiplay reason: String, _ shouldShow: Bool, completion: (evaluationSuccessfull: Bool) -> ()) {
if shouldShow {
let car = LAContext()
guard car.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil) else {
completion(evaluationSuccessfull: false)
return
}
car.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {(success, error) in
guard error != nil else {
completion(evaluationSuccessfull: false)
return
}
completion(evaluationSuccessfull: success)
}
} else{
completion(evaluationSuccessfull: false)
}
}