dispatch_async() 与 throwables swift 2 Xcode 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31666083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:33:45  来源:igfitidea点击:

dispatch_async() with throwables swift 2 Xcode 7

xcodeasynchronouserror-handlingswift2xcode7-beta3

提问by justin shores

Trying to use dispatch_async in which i need a throwable call, but Swift's new error handling, and method calls are confusing me, if anyone could show me how to do this correctly, or point me in the right direction, I would greatly appreciate it.

尝试使用 dispatch_async 其中我需要一个 throwable 调用,但 Swift 的新错误处理和方法调用让我感到困惑,如果有人能告诉我如何正确地做到这一点,或者指出我正确的方向,我将不胜感激。

Code:

代码:

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)
        }
    }
}

Warning:

警告:

: Invalid conversion from throwing function of type '() throws -> _' to non-throwing function type '@convention(block) () -> Void'

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

回答by matt

FINAL EDIT: This bug is fixed in Swift 2.0 final (Xcode 7 final).

最终编辑:此错误已在 Swift 2.0 最终版(Xcode 7 最终版)中修复。

Change

改变

} catch let error as NSError {

to

} catch {

The effect is exactly the same — you can still print(error)— but the code will compile and you'll be on your way.

效果完全一样——你仍然可以print(error)——但代码会编译,你会继续前进。

EDITHere's why I think (as I said in a comment) that what you've found is a bug. This compiles just fine:

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

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

The compiler does not complain, and in particular does not force you to write func test() throws— thus proving that the compiler thinks this catchis exhaustive.

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

But this does not compile:

但这不能编译:

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

But it's the exact same do/catchblocks! So why doesn't it compile here? I think it's because the compiler is somehow confused by the surrounding GCD block (hence all the stuff in the error message about the @convention(block)function).

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

So, what I'm saying is, either they should both compile or they should both fail to compile. The fact that one does and the other doesn't is, I think, a bug in the compiler, and I have submitted a bug report on precisely that basis.

所以,我要说的是,要么它们都应该编译,要么它们都不能编译。我认为,一个有而另一个没有的事实是编译器中的一个错误,我正是在此基础上提交了一份错误报告。

EDIT 2: Here's another pair that illustrates the bug (this comes from @fqdn's comment). This does notcompile:

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

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

        }
    }
}

But this doescompile even though it is exactly the same thing:

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

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

        }
    }
    dispatch_async(dispatch_get_main_queue(), inner)
}

That inconsistency is the bug.

这种不一致就是错误。