1

I have looked Here for solutions to this, and have tried several, none which worked. Maybe this problem I am having can help some people who did not get a solution from the link I provided.

I am trying to set up a camera, however, I am getting the following when I run the app.

Unexpectedly found nil while unwrapping an Optional value

this happens on the line indicated below:

        func setupInputOutput() {
//        if currentCamera != nil {
            do {
                let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!) //Here error happens
                captureSession.addInput(captureDeviceInput)
                photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
            } catch {
                print("error -- \(error)")
            }
//        }
    }

I declare currentCamera as shown below:

    var currentCamera: AVCaptureDevice?
  • You are just declaring the "currentCamera". You should also initialize it. currentCamera = AVCaptureDevice.default(for: AVMediaType.video) – bseh Aug 10 '18 at 07:43
  • you can refer from : https://stackoverflow.com/a/28756857/7334409 – Sagar Bhut Aug 10 '18 at 07:45
  • yo have not assigned any value to currentCamera so its nil, also check the value using if condition before assessing it. – vivekDas Aug 10 '18 at 07:46

3 Answers3

2

Before initializing your AVCaptureDeviceInput get default AVCaptureDevice and set it into your currentCamera property like this

guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
    for: .video, position: .back) else {
    return
}
self.currentCamera = device
Taras Chernyshenko
  • 2,729
  • 14
  • 27
1

You are getting a crash because you are force unwrapping currentCamera with ! when it might be nil, at this point, with your commented lines you are unsure of whether it is nil or not.

Firstly, you should make sure that your AVCaptureDevice has been initialized prior to getting to this point by doing

currentCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)

In swift generally you should not use == nil and != nil. This is the exact thing that optionals were made for and swift has it's own ways of dealing with it.

    func setupInputOutput() {
        if let currentCamera = currentCamera {
            do {
                let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!) //Here error happens
                captureSession.addInput(captureDeviceInput)photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
            } catch {
                print("error -- \(error)")
            }
        }
    }

If you are unfamiliar with options and unwrapping, you should have a look at the Apple Documentation about it, which can be found here: https://developer.apple.com/documentation/swift/optional

Alan S
  • 594
  • 3
  • 13
1
var currentCamera: AVCaptureDevice?

When you use AVCaptureDevice? the ? means it's an optional var. currentCamera could be an AVCaptureDevice or it could be nil

3 ways to deal with optionals:

// Using Guard
guard let currentCam = currentCamera else {return}
// do stuff using currentCam

// Using if
if let currentCam = currentCamera {
   // do stuff using currentCam
}

// Ugliest
if currentCamera != nil {
   // do stuff using currentCam
}
shayegh
  • 302
  • 1
  • 9