2016-06-12 80 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