我在我的 iOS 应用程序中使用 AFNetworking,对于它发出的所有 GET 请求,我从基本 URL 构建 url,然后使用 NSDictionary 键值对添加参数。
问题是不同的值需要相同的键。
这是我需要最终 URL 的示例 -
http://example.com/.....&id=21212&id=21212&id=33232
在 NSDictionary 中不可能在相同的键中有不同的值。所以我尝试了 NSSet 但没有成功。
let productIDSet: Set = [prodIDArray]
let paramDict = NSMutableDictionary()
paramDict.setObject(productIDSet, forKey: "id")
最佳答案
您只需要 URLComponents
(或 Obj-C 中的 NSURLComponents)。基本思想是为您的 ID 创建一堆查询项。您可以将以下代码粘贴到 playground 中:
import Foundation
import XCPlayground
let queryItems = [URLQueryItem(name: "id", value: "1"), URLQueryItem(name: "id", value: "2")]
var urlComps = URLComponents(string: "www.apple.com/help")!
urlComps.queryItems = queryItems
let result = urlComps.url!
print(result)
你应该看到输出
www.apple.com/help?id=1&id=2
关于ios - 如何在 Swift 中使用包含同一键的多个值的查询参数构建 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34060754/