The error is: "fatal error: unexpectedly found nil while unwrapping an Optional value"
I am doing the following in ViewController:
var imageURL:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string:"http://cdn.businessoffashion.com/site/uploads/2014/09/Karl-Lagerfeld-Self-Portrait-Courtesy.jpg")
let data = NSData(contentsOfURL:url!)
if data!= nil {
imageURL.image = UIImage(data:data!)
}
}
I really don't understand why it will report an error on
imageURL.image = UIImage(data:data!)
while I already told it not to proceed if data is nil. It is not the problem of the link. Nor is there problem with the "data". I tried to print it and it was not nil.
Your code is right, just use:
You can use SDWebImage for display image from URL https://github.com/rs/SDWebImage
Try writing:
instead:
Compiler is maybe confusing exclamation mark with operation to unwrap the optional value.
Swift 3.0 for downloading image and parsing json using the URLSession and URLRequest.
This is the code for it. I have used this you do not need to include classes or anything else. Just use this extension. This is very fast.
Here I have an approach from which you will not get any kind of crash when downloading image. Now currently you are using the following code:
Now change the code with this one, If you are continue with this approach:
And I am sure that If you are used it your nil crash will be solved.