How can I remove the "&" that keep appearing when the URL is printed?

123 Views Asked by At

How can I remove the "&" that keep appearing when the URL is printed? Why is it even appearing?

var url = URLComponents()
    url.scheme = "http"
    url.host = "api.openweathermap.org"

    url.queryItems = [
        URLQueryItem(name: "/data", value: ""),
        URLQueryItem(name: "/2.5", value: ""),

        URLQueryItem(name: "/weather?", value: ""),
        URLQueryItem(name: "lat", value: "35"),
        URLQueryItem(name: "lon", value: "-139")
    ]

    print(url.string!)

// http://api.openweathermap.org?/data=&/2.5=&/weather?=&lat=35&lon=-139

New Issue:

How can I turn the following coordinate into a string?

(currentLocation.coordinate.latitude)

1

There are 1 best solutions below

4
Jb31 On
/data/2.5/weather 

is the path, not a query item. Try:

var url = URLComponents()
url.scheme = "http"
url.host = "api.openweathermap.org"
url.path = "/data/2.5/weather"

url.queryItems = [

    URLQueryItem(name: "lat", value: "35"),
    URLQueryItem(name: "lon", value: "-139")
]