dispatch_async()with throwables swift 2 Xcode 7

jus*_*res 9 error-handling xcode asynchronous swift2 xcode7-beta3

试图使用dispatch_async我需要一个throwable调用,但是Swift的新错误处理和方法调用让我感到困惑,如果有人能告诉我如何正确地做这件事,或者指出我正确的方向,我将非常感激.

码:

func focusAndExposeAtPoint(point: CGPoint) {
    dispatch_async(sessionQueue) {
        var device: AVCaptureDevice = self.videoDeviceInput.device

        do {

            try device.lockForConfiguration()
            if device.focusPointOfInterestSupported && device.isFocusModeSupported(AVCaptureFocusMode.AutoFocus) {
                device.focusPointOfInterest = point
                device.focusMode = AVCaptureFocusMode.AutoFocus
            }

            if device.exposurePointOfInterestSupported && device.isExposureModeSupported(AVCaptureExposureMode.AutoExpose) {
                device.exposurePointOfInterest = point
                device.exposureMode = AVCaptureExposureMode.AutoExpose
            }

            device.unlockForConfiguration()
        } catch let error as NSError {
            print(error)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

警告:

:从抛出函数类型'()抛出 - > _'到非投掷函数类型'@convention(block)() - > Void'的转换无效

mat*_*att 11

最终编辑:此错误在Swift 2.0 final(Xcode 7 final)中得到修复.

更改

} catch let error as NSError {
Run Code Online (Sandbox Code Playgroud)

} catch {
Run Code Online (Sandbox Code Playgroud)

效果完全相同 - 你仍然可以print(error)- 但代码将编译,你将在你的路上.

编辑这就是为什么我认为(正如我在评论中所说)你发现的是一个错误.编译得很好:

func test() {
    do {
        throw NSError(domain: "howdy", code: 1, userInfo:nil)
    } catch let error as NSError {
        print(error)
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器不会抱怨,特别是不会强迫您编写func test() throws- 从而证明编译器认为这catch是详尽无遗的.

但这不编译:

func test() {
    dispatch_async(dispatch_get_main_queue()) {
        do {
            throw NSError(domain: "howdy", code: 1, userInfo:nil)
        } catch let error as NSError {
            print(error)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它是完全相同的do/catch块!那么为什么不在这里编译呢?我认为这是因为编译器在某种程度上被周围的GCD块混淆了(因此关于@convention(block)函数的错误消息中的所有内容).

所以,我所说的是,要么它们都应该编译,要么它们都应该无法编译.我认为,一个人做了,另一个做不到的事实是编译器中的一个错误,我已经提交了一个错误报告.

编辑2:这是另一对说明错误(这来自@ fqdn的评论).这并不会编译:

func test() {
    dispatch_async(dispatch_get_main_queue()) {
        do {
            throw NSError(domain: "howdy", code: 1, userInfo:nil)
        } catch is NSError {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这确实可以编译,即使它完全相同:

func test() {
    func inner() {
        do {
            throw NSError(domain: "howdy", code: 1, userInfo:nil)
        } catch is NSError {

        }
    }
    dispatch_async(dispatch_get_main_queue(), inner)
}
Run Code Online (Sandbox Code Playgroud)

这种不一致是错误.