I’m a little ashamed to post this as I use URLs all the time but this one has me stumped!
I keep getting an ‘Unexpectedly found nil while unwrapping an Optional value’
Just to add some context, I use the same format for over 20 URLs which is making requests to an API. The only difference is the ID at the end of the URL.
Ignore the insecure URL as this is simply a debug endpoint and has no bearing on the issue as I use the same URL in other calls. Also, ignore the first part of the URL, I have purposefully changed it for security purposes.
The code I use is as follows:
func returnEventParticipantsEndPoint(eventID: String) -> URLRequest {
print("URL: (debugEndPointURL)/api/friendgroups/(eventID)")
var url: URL!
url = URL(string: "(debugEndPointURL)/api/friendgroups/(eventID)")!
let requestURL = URLRequest(url: url)
return requestURL
}
The URL printed in the console (top line) before I try to return the URLRequest prints as follows:
http://blahblahblah.blah.net/api/friendgroups/1c8264b95884409f90b4fd8ba9d830e1
Yet, it keeps returning nil and I’d really appreciate some help with this…thanks.
2
Answers
Have you copied and pasted the format for this? Examining the string you have some non printable Unicode characters after the words
api
andfriendgroups
. Retyping the url path and trying again worked for meThe reason that it is failing is that there are hidden Unicode characters in your string. Copying and pasting the url string you provided into a hidden Unicode character checker gives the following for your url
Notice the additional hidden characters at the end of the words
api
andfriendgroups
You can try it here https://www.soscisurvey.de/tools/view-chars.php