canEvaluatePolicy Extra argument 'error' in call Swift Xcode 7

时间:2015-06-15 14:47:34

标签: ios xcode ios8 xcode7

I work with Xcode 7 with swift and I would use the Touch Id. Only I have a error when I use canEvaluatePolicy. I understand my error, I call an argument too. Only if I do not call, it makes me a error because I did not manage my error ... Here are my error and my code: PS: sorry for my bad English.

Error : Extra argument 'error' in call

or

Error : Call can throw, but it is not marked with 'try' and the error is not handled

My code :

import Foundation
import UIKit
import LocalAuthentication

class touchid : UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var lblTouchId: UILabel!

    override func viewDidLoad() {
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

    }




    @IBAction func authenticateWithTouchID(sender: AnyObject) {

        let authenticationObject = LAContext()
        self.pleaseWait()
        if authenticationObject.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics) {
            authenticationObject.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Access", reply: {(Bool, authenticationError) in

                if authenticationError != nil {
                // Authentification annulé ou Touch id non disponible
                    self.lblTouchId.text = "annulé ou touch id non disponible"
                    self.clearAllNotice()
                    self.errorNotice("Erreur !")
                }
                else {
                if Bool == true {
                    self.lblTouchId.text = "authentification réussi"
                    self.clearAllNotice()
                    self.successNotice("Succès !")
                }
                else {
                    self.lblTouchId.text = "echec de l'authentification"
                    self.clearAllNotice()
                    self.errorNotice("Erreur !")
                }
                }
                }
            )
        }
    }
}

3 个答案:

答案 0 :(得分:1)

As mentioned in Using Swift with Cocoa and Objective-C, all Objective-C methods that use NSError to return an error object will now throw when called from Swift 2.0, so you need to use:

do {
    try method()
} catch let error as NSError {
    reportError(error)
}

Removing the reference to NSError in the method() call.

答案 1 :(得分:1)

看着Apple's documentation,似乎有些事情发生了。方法签名是:

func canEvaluatePolicy(_ policy: LAPolicy, error error: NSErrorPointer) -> Bool

奇怪的是,目前该方法没有throw任何东西,因此不需要将它放在do-try块中。我不知道(但我的猜测是肯定的)这个框架仍在调整Swift 2.0,但我想我记得它在Xcode / Swift beta迭代中的某个点实现了throw。有一次,编译器表现得有点不稳定并说:

1 - 方法签名只有1个参数,但是实现了throws(它没有) 2 - 但如上所述,当你这样做时,编译器会给你不同的错误。

有一点需要指出的是,在撰写本文时,当前的方法签名有第二个参数是NSErrorPointer,而不是NSError?宾语。像现在的Apple文档建议那样对待它,所以添加如下内容:

var error: NSErrorPointer?
let canEvaluatePolicty = LAContext().canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: error!)

你的代码应该编译。当beta的更多迭代出现时,关注这个框架可能是一个好主意。

祝你好运!

答案 2 :(得分:0)

尽管g_blott的答案确实可以编译,但是当您不确定会发生什么情况时,强制解开不是一个好习惯。

此代码也可以编译,但不会强制解开任何内容并处理错误的存在:

    var error: NSError?
    let errorPointer: NSErrorPointer = NSErrorPointer(&error)
    let canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthentication, error: errorPointer)
    if let error = error {
        print("Oh noes, an error! ", error)
    } else if canEvaluate {
        print("Can evaluate")
    } else {
        print("Can't evaluate")
    }