Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 Extra argument error in call viewdidload image

Tags:

ios

xcode7

swift2

i'm updating my app to Swift 2 with Xcode 7. this is my code of a ViewController 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!)
}

on line

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

i get the error Extra argument error in call. I already tried with the method do{} and catch{} but it didn't work, i always get that error.

How can i fix that? Thanks

like image 724
markutus Avatar asked Jun 29 '15 18:06

markutus


1 Answers

Swift 2 introduced new error handling. To solve the problem you are having, you need to catch the error instead of passing an NSError object to the AVCaptureDevice method:

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

For a more in-depth explanation have a look at this article:

Error Handling in Swift 2.0

like image 107
sbarow Avatar answered Sep 18 '22 17:09

sbarow