skip to Main Content

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


  1. Have you copied and pasted the format for this? Examining the string you have some non printable Unicode characters after the words api and friendgroups. Retyping the url path and trying again worked for me

    Login or Signup to reply.
  2. The 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

    http://blahblahblah.blah.net/apiU+200B​/friendgroups​U+200B/1c8264b95884409f90b4fd8ba9d830e1
    

    Notice the additional hidden characters at the end of the words api and friendgroups

    You can try it here https://www.soscisurvey.de/tools/view-chars.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search