SchwiftyUI/NewsApiApp

Fatal error: Unexpectedly found nil while unwrapping an Optional value

charlotzzz opened this issue · 0 comments

I kept on getting this fatal error for let url = URL(string: urlString)! in this function in UrlImageModel:

func loadImageFromUrl() {

            guard let urlString = self.urlString else {
            return
        }

            let url = URL(string: urlString)!
            let task = URLSession.shared.dataTask(with: url, completionHandler: self.getImageFromResponse(data:response:error:))
            task.resume()
    }

Why can an optional still be nil even if we used guard? Also, when I move my cursor over urlString, it shows a highlight of my intended url from the API. Does anyone know what is wrong? Thanks in advance!