ios - 无法将类型 '() -> Void' 的值分配给 '(() -> Void)!'

标签 ios swift casting swift3

因为我使用 Swift 3 出现以下错误..

Cannot assign value of type '(LLSimpleCamera?, NSError?) -> Void' to type '((LLSimpleCamera?, Error?) -> Void)!'

有人知道怎么办吗? 这是我的代码..

camera.onError = { (camera: LLSimpleCamera?, error: NSError?) -> Void in
        print("Camera error: \(error)")

        if error.domain == LLSimpleCameraErrorDomain {
            if error.code == Int(LLSimpleCameraErrorCodeCameraPermission.rawValue) || error.code == Int(LLSimpleCameraErrorCodeMicrophonePermission.rawValue) {

                let alertVC = UIAlertController(title: "Ooops!", message: "We need permission for the camera. Please go to your settings.", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                let settingsAction = UIAlertAction(title: "Settings", style: .default) { (action) in
                    UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
                }

                alertVC.addAction(okAction)
                alertVC.addAction(settingsAction)

                self.present(alertVC, animated: true, completion: nil)
            }
        }
    }

最佳答案

Swift 3 将 Objective C NSError 类型映射到协议(protocol) Error(aka ErrorTypeSwift 2 中)。

因此在闭包参数列表中,它希望将 Error 作为第二个参数的类型,而不是 NSError

但是在闭包内部你需要输入 case error 参数到 NSError 如果你想使用 .domain/.code /等

关于ios - 无法将类型 '() -> Void' 的值分配给 '(() -> Void)!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875485/

相关文章:

c# - 使用 MVVMCross 的属性条件绑定(bind)

iphone - 通过 UIDocumentInteractionController 将 pdf 文件添加到 iBooks 但不打开文件

java - Java 中的继承、泛型和转换

c++ - 在签名和未签名之间进行转换

ios - 当 iOS 应用程序处于后台时进行定期网络调用

ios - 是否自动布局?如何定位 3 个 subview ?

ios - 自定义iOS导航栏高度,添加背景,菜单按钮(Swift)

ios - 你能在 NSObject 类中将 Target 添加到 UIButton 吗?

ios - 检查 UIViewController 是否有某个变量

C# 如何解决 Func<T, bool> 中的逆变问题?