Type 'AVCaptureDevice' has no member 'defaultDevice'

Brandon Ruetsche picture Brandon Ruetsche · Oct 26, 2017 · Viewed 12.2k times · Source

Working on a QR code reader. I am new to programming so this might be an easy fix. The error is "Type 'AVCaptureDevice' has no member 'defaultDevice'" Thanks for the help in advance!

 //Creating session
    let session = AVCaptureSession()
    //Define capture device
    let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    do
    {
        let input = try AVCaptureDeviceInput(device: captureDevice)
        session.addInput(input)
    }

Answer

rmaddy picture rmaddy · Oct 26, 2017

You are using the old Swift 2 API. The line:

let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

should be:

let captureDevice = AVCaptureDevice.default(for: .video)