user1060500
user1060500

Reputation: 1535

URL Object is nil after creating it with let constant string url

Why does the first example give me a nil and the second example give me a valid URL object. I am trying to use this jsonUrlString constant and it's not working.

1)

    let jsonUrlString = """
                            https://myURI.domain.com/
                       """

    let temp = URL(string: jsonUrlString)

2)

    let temp = URL(string: "https://myURI.domain.com/")

Upvotes: 0

Views: 29

Answers (1)

rmaddy
rmaddy

Reputation: 318824

The first example has lots of extra spaces in the URL so it's not valid.

It's the same as:

let temp = URL(string: "                        https://myURI.domain.com/")

It would work if you had:

let jsonUrlString = """
https://myURI.domain.com/
"""

This defines the string without the extra spaces.

Upvotes: 1

Related Questions