ios - fatal error: unexpectedly found nil while unwrapping an Optional value in AVFoundation -
ios - fatal error: unexpectedly found nil while unwrapping an Optional value in AVFoundation -
i making application uses flash lite torch. writing next code , xcode giving me error on first line. can please suggest root cause of it? done debugging , can't seem find it..
@ibaction func powerbutton(sender: uibutton) { nslog("flashbuttonclicked") allow flashlight : avcapturedevice = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo)! torchbutton.setbackgroundimage(switchonimage, forstate:uicontrolstate.normal) nslog("button background changed!!") if flashlight.torchavailable && flashlight.istorchmodesupported(avcapturetorchmode.on) { nslog("inside if...") allow success = flashlight.lockforconfiguration(nil) if (success) { if flashlight.torchactive { flashlight.istorchmodesupported(avcapturetorchmode.off) onoffbutton.setbackgroundimage(switchoffimage, forstate:uicontrolstate.normal) } else { flashlight.settorchmodeonwithlevel(100, error: nil) onoffbutton.setbackgroundimage(switchonimage, forstate:uicontrolstate.normal) } flashlight.unlockforconfiguration() } } }
avcapturedevice.defaultdevicewithmediatype(avmediatypevideo)
returning nil object. you're attempting disclose (!
) , assigning non-nillable constant let flashlight : avcapturedevice
try adding check nil
var flashlight = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo) if flashlight != nil { //rest of code here }
ios iphone swift avfoundation fatal-error
Comments
Post a Comment