我在view controller中使用相机时正在实现此方法

let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

//... code
if let device = captureDevice {
            do {
                if (try device.lockForConfiguration()) {
                    device.focusPointOfInterest = focusPoint
                    device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus
                    device.exposurePointOfInterest = focusPoint
                    device.exposureMode = AVCaptureExposureMode.ContinuousAutoExposure
                    device.unlockForConfiguration()
                }
            }
            catch {
                print("Error")
            }
        }
//... code

尝试转换为Swift 2.0时,我发现此错误Type '()' does not conform to protocol to 'BooleanType'在第
if (try device.lockForConfiguration())

实际上,我正在尝试找出解决方法,如何将其设置为'BooleanType'?用Swift 1.2我的代码很简单
if (device.lockForConfiguration())

提前致谢。

最佳答案

看起来lockForConfiguration返回Void并抛出,因此返回值不符合BooleanType。

我认为以下代码应为您工作:

if let device = captureDevice {
    do {
        try device.lockForConfiguration()
        device.focusPointOfInterest = focusPoint
        device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus
        device.exposurePointOfInterest = focusPoint
        device.exposureMode = AVCaptureExposureMode.ContinuousAutoExposure
        device.unlockForConfiguration()
    }
    catch {
        print("Error")
    }
}

10-08 05:25