【发布时间】:2016-10-05 13:35:15
【问题描述】:
由于我使用 Swift 3 出现以下错误..
无法将类型 '(LLSimpleCamera?, NSError?) -> Void' 的值分配给 输入'((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)
}
}
}
【问题讨论】:
-
您可以尝试将
camera.onError = { (camera: LLSimpleCamera?, error: NSError?) -> Void in …替换为camera.onError = { (camera: LLSimpleCamera?, error: Error?) -> Void in …吗? -
是的,你是对的!但是现在 Xcode 说错误没有类型的域和代码.. 你知道如何解决这个问题吗?
-
好吧,我猜你必须使用
as?将错误转换为NSError。问题是在 Swift 3 之前有协议ErrorType和旧的 Objective C 类NSError,但在 Swift 中,在命名简化的方式上,两者现在都称为Error(它没有域和其他属性),并且所有 @ 987654329@s 个 ObjC 映射到它。 -
好的,我会将之前的评论信息复制到答案中,只是为了回答问题。