ios - fatal error: unexpectedly found nil while unwrapping an Optional value in AVFoundation -


I am making an application that uses Flash Light as torch, I am writing the following code and XCode I am giving an error on the first line, can anyone suggest its root cause? I have been debugging and there is no problem finding it.

  @IBAction func powerButton (Sender: UIButton) {NSLog ("FlashButtonClicked") Flash Light: AVCaptureDevice = AVCaptureDevice DefaultDeviceWithMediaType (AVMediaTypeVideo)! TorchButton.setBackgroundImage (switchOnImage, forState: UIControlState.Normal) NSLog ("Change Button Background!") If flashLight.torch is available & amp; Flashlight.isTorchModeSupported (AVCaptureTorchMode.On) {NSLog ("if inside ...") success = flashlight.lockForConfiguration (zero) if (success) {if flashLight.torchActive {flashLight.isTorchModeSupported (AVCaptureTorchMode.Off) onOffbutton.setBackgroundImage (switchoff Image, State: UIControlState.Normal)} and {Flashlight.SetterMeModeOut with Level (100, Error: Zero) onoffbutton.setBackgroundImage (switchonMage, forState: UIControlState.Normal)} Flashlight.unlockForConfiguration ()}}}  

AVCaptureDevice.defaultDeviceWithMediaType (AVMediaTypeVideo) returning a null object Opening it (! ) Try it and give it a non-nylable stable light given: AVCaptureDevice

Try adding a check for zero

  Var Flash Light = AVCaptureDevice.defaultDeviceWithMediaType (AVMediaTypeVideo) if Flash Lite! = Zero {// the rest of your code here}  

Comments