1

I want to fetch an image from server URL in to my bar button item. I am using code like below but it is gives an error : Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value . how can I solve this problem ? Note: loadImg func is an extension and its working another controller. and serverURL is my class for profile data.

let imageView = UIImageView()
imageView.loadImg(url: URL(string: self.serverURL.ppUrl!)!) // this line give error
let customView = UIButton(type: .system)
customView.setImage(imageView.image, for: .normal)
customView.backgroundColor = .white

let barButtonItem = UIBarButtonItem(customView: customView)
navigationItem.leftBarButtonItem = barButtonItem

extension UIImageView {
    public func loadImg(url: URL) {
        DispatchQueue.global().async { [weak self] in
            if let data = try? Data(contentsOf: url) {
                if let image = UIImage(data: data) {

                    DispatchQueue.main.async {
                        self?.image = image
                    }
                }
            }
        }
    }
}
koen
  • 5,383
  • 7
  • 50
  • 89
SwiftTry
  • 121
  • 10

1 Answers1

0

You are force unwrapping two optionals (marked with the !). If one or both of them are nil, you will get a crash. So you need to check first if they exist, and bail otherwise.

Something like this:

let imageView = UIImageView()
if let urlString = self.serverURL.ppUrl as? String, let url = URL(string: urlString) as? URL {

    imageView.loadImg(url: url)
    ...
}
koen
  • 5,383
  • 7
  • 50
  • 89
  • thank you I tried it but still same error. I will check my data – SwiftTry Nov 22 '19 at 19:20
  • You can find more info about this error here: https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu – koen Nov 22 '19 at 19:22
  • And what is `loadImg` ? – koen Nov 22 '19 at 19:25
  • loadImg is an extension for fetch image like url string from server data. it is working in other controllers. And in this controllers I use two optionals (marked with !) but it is working. – SwiftTry Nov 22 '19 at 19:34
  • and "if let " modality, safety than my "unwrapping two optionals" its it right ? I am new with swift programming – SwiftTry Nov 22 '19 at 19:37
  • Please add the code for `loadImg` to your question. Crash could occur there as well. The fact you don't have the crash in other cases only means that your optionals are not nil. – koen Nov 22 '19 at 19:40
  • I added it loadImg func – SwiftTry Nov 22 '19 at 19:43
  • I added some updates to my answer, can you try now? I don't see anything suspicious in `imgLoad`. – koen Nov 22 '19 at 19:45
  • Still same error. I making a mistake somewhere but I don't know. May is relative about bar button item ? Should I try with "didSet" method what you think ? – SwiftTry Nov 22 '19 at 19:48
  • What line do you get the error now? Are you 200% sure that `self.serverURL.ppUrl` is a string? – koen Nov 22 '19 at 19:50
  • Yes exactly that line – SwiftTry Nov 22 '19 at 19:51