【问题标题】:Basic Touch ID Implementation基本的 Touch ID 实现
【发布时间】:2016-06-12 07:01:13
【问题描述】:

我一直想写一个嵌套函数,它接受 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() 我收到一条错误消息

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

我做错了什么?

【问题讨论】:

    标签: ios swift touch-id


    【解决方案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.修复返回的闭包

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

    【讨论】:

    • 和@luk2302,这也可以被认为是一个完成处理程序对吗?
    • @Dershowitz123 是的,它很像一个完成处理程序。
    • 完美。这是否也是一个同步过程?因为我读过同步进程冻结了 UI?我错了吗? @luk2302?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多