我正在尝试创建一个类似它的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: ???????),
]
Run Code Online (Sandbox Code Playgroud)
如何添加数组数据以获得所需的完整URL? https://api.website.com/products?product_ids=1,2,3,4,5
您只需要将数组元素映射到字符串中,然后使用逗号将结果连接起来即可:
let value = array.map(String.init).joined(separator: ",")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
153 次 |
| 最近记录: |