Ios Swift 2.0类型';()';不符合协议
我在Ios Swift 2.0类型';()';不符合协议,ios,swift,swift2,Ios,Swift,Swift2,我在视图控制器中使用相机时实现此方法 let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) //... code if let device = captureDevice { do { if (try device.lockForConfiguration()) { device.focus
视图控制器中使用相机时实现此方法
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'()'不符合第行的“BooleanType”
协议
if (try device.lockForConfiguration())
事实上,我正在想办法解决这个问题,我怎样才能把它变成一个“布尔类型”
?在Swift 1.2中,我的代码是
if (device.lockForConfiguration())
提前感谢。看起来lockForConfiguration返回Void和throws,所以返回值不符合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")
}
}
是的,你是对的。我刚刚意识到,如果try
语句失败,它将跳到catch
不执行代码,否则将正常继续。