Convert dictionary to query string in swift?

前端 未结 11 876
走了就别回头了
走了就别回头了 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:11

    Same as @KKRocks with update for swift 4.1.2

    func queryItems(dictionary: [String:Any]) -> String {
        var components = URLComponents()
        print(components.url!)
        components.queryItems = dictionary.map {
            URLQueryItem(name: $0, value: String(describing: $1))
        }
       return (components.url?.absoluteString)!
    }
    

提交回复
热议问题