Convert dictionary to query string in swift?

前端 未结 11 854
走了就别回头了
走了就别回头了 2021-01-04 04:37

I have a dictionary as [String:Any].Now i want to convert this dictionary keys & value as key=value&key=value.I have created below extensio

11条回答
  •  庸人自扰
    2021-01-04 05:04

    For anyone that wants the "short, short" version.

    Condensed using map, you don't need the forEach or for loops. Also protocol constrained for type enforcement on the dictionary.

    extension Dictionary where Key : StringProtocol, Value : StringProtocol {
        var queryString: String {
            self.map { "\($0)=\($1)" }.joined(separator: "&")
        }
    }
    

提交回复
热议问题