The quickest solution: just use the stringy method...
.replacingOccurrences(of: " ", with: "%20")
...at the end of your string, and it will do as it says.
Apple should be EMBARRASSED that their URL(string: ) method doesn't automatically do this (and more). It's 2023 and I wasted 3 hours finding this half-assed solution.
My specific example was inside cellForItemAt and having to do with optionals.
let stringOfImageURLFromFirebase = wordController?.word?.images[indexPath.item].imgUrl
let stringWithNoSpaces = string?.replacingOccurrences(of: " ", with: "%20")
if let imageURLString = stringWithNoSpaces,
let imageURL = URL(string: imageURLString) {
cell.definitionImageView.loadImageFromFirebase(url: imageURL)
} else {
cell.definitionImageView.image = UIImage(named: "slictionarylogo")
}
You can fuddle with .addingPercentEncoding for a long time, try NSURL nonsense, and you'll just be faster with using the obvious. Apple sucks, and should've made this painfully easy since it's such a common problem. Again, they are asleep at the wheel, and their documentation is AWFUL too.