【问题标题】:How to create url from int array如何从 int 数组创建 url
【发布时间】:2018-01-30 02:20:24
【问题描述】:

我正在尝试创建一个类似的 URL https://api.website.com/products?product_ids=1,2,3,4,5 使用 CoreData 中的整数数组

到目前为止我确实有这个代码

let array = [1,2,3,4,5]

var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "api.website.com"
urlComponents.path = "/products"
urlComponents.queryItems = [
    URLQueryItem(name: "product_ids", value: ???????),
]

如何添加数组数据以获得所需的完整 URL? https://api.website.com/products?product_ids=1,2,3,4,5

【问题讨论】:

标签: ios swift


【解决方案1】:

您只需将数组元素映射为字符串并使用逗号连接结果:

let value = array.map(String.init).joined(separator: ",")

【讨论】:

    猜你喜欢
    • 2016-06-01
    • 1970-01-01
    • 2016-05-02
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多