3

There is an error for optional while downloading the image from the firebase storage, I am converting the string to URL to download the image

here is the code where the error is occuring , if any more code is required do let me know

let imageUrl = URL(string: post._postuserprofileImagUrl)
        ImageService.getImage(withURL: imageUrl) { image in
            self.profileImageView.image = image
        }
  • Try put "!" after post._postuserprofileImagUrl like post._postuserprofileImagUrl! to force unwrap it, or post._postuserprofileImagUrl ?? "". Check if this works? – Huzaifa ameen Sep 10 '19 at 14:59
  • no, it is pointing to ( withURL: imageUrl ) and giving message of "Value of optional type 'URL?' must be unwrapped to a value of type 'URL'" –  Sep 10 '19 at 15:09

1 Answers1

2

You have to (safely) unwrap the URL instance

if let imageUrl = URL(string: post._postuserprofileImagUrl) {
        ImageService.getImage(withURL: imageUrl) { image in
            self.profileImageView.image = image
        }
}

Or even (if postuserprofileImagUrl is optional, too)

if let userprofileImagUrl =  post._postuserprofileImagUrl,
   let imageUrl = URL(string: userprofileImagUrl) {
        ImageService.getImage(withURL: imageUrl) { image in
            self.profileImageView.image = image
        }
}
vadian
  • 274,689
  • 30
  • 353
  • 361