辞書 → クエリ文字列(URLパラメーター)にコンバート
extension Dictionary where Key : StringProtocol, Value : StringProtocol {
var queryString: String {
var components = URLComponents()
components.queryItems = self.map {
URLQueryItem(name: $0 as! String, value: String(describing: $1))
}
return (components.url?.absoluteString)!
}
}
使用法
let queryDic = ["key1": "value1", "key2": "value2"]
print(queryDic.queryString)