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
  • 253,546
  • 28
  • 306
  • 323
  • i guess this has been covered enough here , isn't it ? – Sh_Khan Sep 10 '19 at 15:13
  • @Sh_Khan According to the comments It seems that the code doesn't crash, so it's actually not a duplicate – vadian Sep 10 '19 at 15:16
  • it's not giving any error of Optional but it's not downloading the images yet, let me check whats wrong –  Sep 10 '19 at 15:25