如何删除打印URL时不断出现的“&”?

时间:2018-10-09 18:51:50

标签: swift url nsurlcomponents

如何删除打印URL时不断出现的“&”?为什么还会出现?

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

新问题:

如何将以下坐标转换为字符串?

(currentLocation.coordinate.latitude)

1 个答案:

答案 0 :(得分:2)

/data/2.5/weather 

是路径,而不是查询项。 试试:

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")
]