0

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 Answers1

2
/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")
]
ielyamani
  • 17,807
  • 10
  • 55
  • 90
Jb31
  • 1,381
  • 1
  • 10
  • 19
  • Now I get the error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value –  Oct 09 '18 at 19:02
  • The path needs to start with `/`. `url.path = "/data/2.5/weather"` – rmaddy Oct 09 '18 at 19:06
  • Ok, now I just need to turn the coordinates received by the device into strings, do you know how this can be done? –  Oct 09 '18 at 19:50
  • @JuniorRickson That's a completely new question. Indicate that this question has been solved by clicking the checkmark to the left of the answer. If you have a new question a lot of research and try some things. If you get stuck, post a new question with relevant code and clearly explain your issue. – rmaddy Oct 09 '18 at 20:22