fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

//Set Up Search Bar
      
        let searchBar = resultSearchController!.searchBar
        searchBar.sizeToFit()
        searchBar.placeholder = "Search For Locations And Places"
        navigationItem.titleView = resultSearchController?.searchBar
      
        //UISearchController Set Up

I'm fairly new to coding, so I'm not exactly sure what the issue is. I'm getting the error on the first line of code whenever I run my app. I thought it was the "?" in the last line of code was the problem causing this, but when I replaced it with "!" like the first line, it still didn't work. I tried replacing the "!" with a "?" and it didn't work either. I'm not sure how to solve this. I feel like it may have something to do with the first and last line though. Can anybody maybe explain this to me and help me solve to understand better?

Thanks so much.

-Deb

Answered by QuinceyMorris in 213498022

You are getting the result you asked for. If resultSearchController is an optional value, the "!" operator in the first line means "unwrap the optional to a non-optional value if possible, or crash if the optional is nil". You're crashing, so that means it's nil.

Accepted Answer

You are getting the result you asked for. If resultSearchController is an optional value, the "!" operator in the first line means "unwrap the optional to a non-optional value if possible, or crash if the optional is nil". You're crashing, so that means it's nil.

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
 
 
Q