2015-09-07 78 views
3

用下面的代碼我得到這個錯誤:功能錯誤處理

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

和它在這行代碼:

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

    }) 

} 
+2

我的猜測是斯威夫特2和try/catch VS錯誤,但OP正在忸怩作態來考驗我們。 – zaph

+0

@jtbandes這是錯誤:無法轉換'inout NSError'類型的值? (aka'inout可選')與預期參數類型'()' –

+2

不要告訴我 - 告訴所有人。 [編輯]你的問題,並輸入所需的信息。見[問]。 – jtbandes

回答

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!") 
     } 
    }) 
} 
+0

這真是太好了,謝謝!現在唯一的問題是,它給了我一個錯誤「預期模式」在線'let try locked = device.lockForConfiguration()' –

+0

好吧,我想出來了,上面的答案非常接近。刪除'let try locked ='並用'try'替換。刪除'如果鎖定',它是相應的花括號。而已! –

+0

@Wheeler Preddy更新回答問題。 – zaph