Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function error handling in Swift 2

With the following code I'm getting this error:

Cannot convert value of type 'inout NSError?' (aka 'inout Optional') to expected argument type '()'

and it's on this line of code:

if device.lockForConfiguration(&error)

Here's the rest of the code:

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

    })

}
like image 499
wheelerscott Avatar asked Feb 21 '26 19:02

wheelerscott


1 Answers

InSwift 2 error handling has changed from NSError in-out parameters to try/catch (not exceptions).

I think this is a correct conversion from NSError to 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!")
        }
    })
}
like image 155
zaph Avatar answered Feb 24 '26 14:02

zaph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!