基本触控 ID 实现



我一直想编写一个嵌套函数,该函数接受 touchID 的原因字符串和布尔值(如果是否应该显示)。这是我的代码

 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() 我收到一个错误说

表达式解析为未使用的函数。

我做错了什么?

问题是你的方法presentTouchID返回一个闭包/函数。您调用presentTouchID但不以任何方式使用返回的闭包。

您在这里有几个选择。
1. 调用返回的闭包:

presentTouchID(reasonToDsiplay: "Are you the owner?", true)(true)

这看起来真的很尴尬。
2. 您可以将返回的闭包存储在变量中:

let present = presentTouchID(reasonToDsiplay: "Are you the owner?", true)

我不确定这是否有意义。
3.您可以从presentTouchID
中删除布尔值作为参数4. 修复返回的闭包

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)
    }
}

最新更新