swift - 如何删除打印 URL 时不断出现的 "&"?

标签 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

新一期:

如何将下面的坐标转成字符串?

(当前位置.坐标.纬度)

最佳答案

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

关于swift - 如何删除打印 URL 时不断出现的 "&"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52727583/

相关文章:

ios - 如何在 swift 中将字符索引从布局管理器转换为字符串比例

ios - 如何在使用 BezierPath 绘制的图像中制作圆角

php - Javascript/PHP 中未定义图像大小

ios - 如何快速从POST请求中获取params值

带有 url 参数的 Swift GET 请求

ios - 如何测试 Siren(https ://github. com/ArtSabintsev/Siren) 是否在我的应用程序上正常移动

ios - Xcode Playground 使用简单的 UITableViewController 代码崩溃

url - .htaccess 如何为 seo 友好的 url 编写 htaccess 重写规则

authentication - 通过 URL 对 HTTP 请求进行身份验证