2015-06-29 36 views
8

我正在用Xcode 7更新我的應用到Swift 2。這是我的ViewController viewDidLoad的代碼。Swift 2調用viewdidload中的額外參數錯誤

override func viewDidLoad() { 
     super.viewDidLoad() 

    // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video 
    // as the media type parameter. 
    let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 

    // Get an instance of the AVCaptureDeviceInput class using the previous device object. 
    var error:NSError? 

    let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error) 

    if (error != nil) { 
     // If any error occurs, simply log the description of it and don't continue any more. 
     print("\(error?.localizedDescription)") 
     return 
    } 

    // Initialize the captureSession object. 
    captureSession = AVCaptureSession() 
    // Set the input device on the capture session. 
    captureSession?.addInput(input as! AVCaptureInput) 

    // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session. 
    let captureMetadataOutput = AVCaptureMetadataOutput() 
    captureSession?.addOutput(captureMetadataOutput) 

    // Set delegate and use the default dispatch queue to execute the call back 
    captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) 
    captureMetadataOutput.metadataObjectTypes = supportedBarCodes 

    // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer. 
    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
    videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
    videoPreviewLayer?.frame = view.layer.bounds 
    view.layer.addSublayer(videoPreviewLayer!) 

    // Start video capture. 
    captureSession?.startRunning() 

    // Move the message label to the top view 
    view.bringSubviewToFront(messageLabel) 

    // Initialize QR Code Frame to highlight the QR code 
    qrCodeFrameView = UIView() 
    qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor 
    qrCodeFrameView?.layer.borderWidth = 2 
    view.addSubview(qrCodeFrameView!) 
    view.bringSubviewToFront(qrCodeFrameView!) 
} 

上線

let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error) 

我得到的錯誤額外的參數錯誤呼叫。我已經嘗試過使用do {}和catch {}方法,但它不起作用,我總是得到那個錯誤。

我該如何解決這個問題?由於

+0

Swift 2的一個新特性是拋出而不是接受'NSError'指針,所以這很可能是你的問題。 – sbarow

+0

@sbarow好筆記! – fqdn

+0

@sbarow那麼我如何解決這個問題? – markutus

回答

0

它看起來並不像這種類型的方法上AVCaptureDeviceInput存在了,看看 - >https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVCaptureDeviceInput_Class/index.html#//apple_ref/swift/cl/c:objc(cs)AVCaptureDeviceInput

(它看起來像你可能想使用init(device:)

...作爲一個方便的技巧:無論何時,如果您不確定是否看到最新的「預發佈」版本的文檔,請通過網絡瀏覽開發人員庫,然後在「資料庫」和「/ ios之間添加URL />添加/預發佈'如果需要:)

+0

那麼我需要在代碼中做什麼更改? – markutus

+0

@markutus你能找到我鏈接到上面的初始化方法的新簽名?你能解決你的問題嗎?讓我知道你是否需要任何幫助!我鏈接的文檔應該能夠讓你到那裏 – fqdn

16

Swift 2引入了新的錯誤處理。爲了解決你的問題,你需要catch的錯誤,而不是通過一個NSError對象的AVCaptureDevice方法:

override func viewDidLoad() { 
    super.viewDidLoad() 

    do { 
     let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
     let input = try AVCaptureDeviceInput(device: captureDevice) 
     // Do the rest of your work... 
    } catch let error as NSError { 
     // Handle any errors 
     print(error) 
    } 
} 

爲了更深入的解釋來看看這篇文章:

Error Handling in Swift 2.0