12

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)
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Brandon Ruetsche
  • 121
  • 1
  • 1
  • 3

2 Answers2

27

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

let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

should be:

let captureDevice = AVCaptureDevice.default(for: .video)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
3

This is swift 3.0

let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

In swift 4.1

AVCaptureDevice.default(for: AVMediaType.video)
{
   let input = AVCaptureDeviceInput(device: captureDevice)
   session.addInput(input)
}

I hope this will help you

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Raja Usman
  • 51
  • 4