-1

App crashes due to nil value in code on line :

if let strURL = (contentEditingInput!.audiovisualAsset as? AVURLAsset)?.url.absoluteString

Unexpectedly found nil while unwrapping an Optional value

func getUrlFromPHAsset(asset: PHAsset, callBack: @escaping (_ url: URL?) -> Void) {
    asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (contentEditingInput, dictInfo) in

        if let strURL = (contentEditingInput!.audiovisualAsset as? AVURLAsset)?.url.absoluteString {
            print("VIDEO URL: \(strURL)")
            callBack(URL.init(string: strURL))
        }
    })
}
Bhaumik
  • 1,218
  • 1
  • 11
  • 20

1 Answers1

0

Just a guess, but this implicitly unwrapped optional is unsafe: contentEditingInput!. Is that what's nil? it would be safer to have that line as:

if let strURL = (contentEditingInput?.audiovisualAsset as? AVURLAsset)?.url.absoluteString

Otherwise, if contentEditingInput is nil, you'll crash when it's unwrapped.

For future reference, please try to provide more information about where the crash is occurring, related code, and what the values are that are used in the code shown.

clarus
  • 2,455
  • 18
  • 19
  • yes i am getting contentEditingInput as nil. I have already used if else part but still giving error – Udit Goyal Apr 03 '19 at 13:30
  • The difference in my code above is the "?" after `contentEditingInput`. If that value is nil, evaluation will stop rather than crash. – clarus Apr 03 '19 at 13:58