2016-06-12 82 views
0

我一直在写一个嵌套函数,它接受touchID的原因字符串和一个布尔值(如果应该显示或不显示)。这是我的代码基本触摸ID实现

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

当我做 func viewDidLoad()presentTouchID(reasonToDsiplay: "Are you the owner?", true)我得到一个错误说

表达式解析为一个未使用的功能。

我在做什么错?

回答

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

而@ luk2302,这也可以看作是一个完成处理程序正确删除布尔作为参数? – Dershowitz123

+0

@ Dershowitz123是的,它是一个完成处理程序。 – luk2302

+0

完美。这是否也是一个同步过程?因为我读过同步过程冻结了用户界面?我是我错了? @ luk2302? – Dershowitz123