How can I build a URL with query parameters containing multiple values for the same key in Swift?

后端 未结 7 1150
一生所求
一生所求 2020-12-08 18:35

I am using AFNetworking in my iOS app and for all the GET requests it makes, I build the url from a base URL and than add parameters using NSDictionary Key-Value pairs.

7条回答
  •  情书的邮戳
    2020-12-08 19:16

    2019

    private func tellServerSomething(_ d: String, _ s: String) {
        
        var c = URLComponents(string: "https://you.com/info")
        c?.queryItems = [
            URLQueryItem(name: "description", value: d),
            URLQueryItem(name: "summary", value: s)
        ]
        guard let u = c?.url else { return print("url fail") }
        do {
            let r = try String(contentsOf: u)
            print("Server response \(r)")
        }
        catch { return print("comms fail") }
    }
    

    Percent-encoding and everything else is handled.

提交回复
热议问题