3

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

    })

}
3
  • 2
    My guess is Swift 2 and try/catch vs error but the OP is being coy to test us. Commented Sep 7, 2015 at 21:37
  • @jtbandes This is the error: Cannot convert value of type 'inout NSError?' (aka 'inout Optional<NSError>') to expected argument type '()' Commented Sep 7, 2015 at 21:37
  • 2
    Don't tell me — tell everyone. Edit your question and put in the required information. See How to Ask. Commented Sep 7, 2015 at 21:38

1 Answer 1

1

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!")
        }
    })
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is great thank you! Only problem now is that it's giving me an error "Expected pattern" on the line let try locked = device.lockForConfiguration()
Ok so I figured it out and the answer above is really close. Remove let try locked = and replace with just try. Remove if locked and it's corresponding curly brackets. That's it!
@Wheeler Preddy Updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.