Swift 2中的函数错误处理

时间:2015-09-07 21:28:08

标签: swift error-handling swift2

使用以下代码我收到此错误:

  

无法转换类型' in out NSError?' (又名' inout Optional')预期参数类型'()'

并且在这行代码中:

if device.lockForConfiguration(&error)

以下是代码的其余部分:

func focusWithMode(focusMode:AVCaptureFocusMode, exposureMode:AVCaptureExposureMode, point:CGPoint, monitorSubjectAreaChange:Bool){

    dispatch_async(self.sessionQueue!, {
        var device: AVCaptureDevice! = self.videoDeviceInput!.device
        var error: NSError? = nil

        if device.lockForConfiguration(&error){
            if device.focusPointOfInterestSupported && device.isFocusModeSupported(focusMode){
                device.focusMode = focusMode
                device.focusPointOfInterest = point
            }
            if device.exposurePointOfInterestSupported && device.isExposureModeSupported(exposureMode){
                device.exposurePointOfInterest = point
                device.exposureMode = exposureMode
            }
            device.subjectAreaChangeMonitoringEnabled = monitorSubjectAreaChange
            device.unlockForConfiguration()
        }

    })

}

1 个答案:

答案 0 :(得分:1)

InSwift 2错误处理已从NSError输入输出参数更改为try / catch(非例外)。

我认为这是从NSError到try / catch:

的正确转换
func focusWithMode(focusMode:AVCaptureFocusMode, exposureMode:AVCaptureExposureMode, point:CGPoint, monitorSubjectAreaChange:Bool){
    dispatch_async(self.sessionQueue!, {
        var device: AVCaptureDevice! = self.videoDeviceInput!.device
        var error: NSError? = nil

        do {
           try device.lockForConfiguration()
            if device.focusPointOfInterestSupported && device.isFocusModeSupported(focusMode){
                device.focusMode = focusMode
                device.focusPointOfInterest = point
            }
            if device.exposurePointOfInterestSupported && device.isExposureModeSupported(exposureMode){
                device.exposurePointOfInterest = point
                device.exposureMode = exposureMode
            }
            device.subjectAreaChangeMonitoringEnabled = monitorSubjectAreaChange
            device.unlockForConfiguration()
        }
        catch {
            print("Locked error!")
        }
    })
}