Convert dictionary to query string in swift?

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

    var populatedDictionary = ["key1": "value1", "key2": "value2"]
    
    extension Dictionary {
        var queryString: String {
            var output: String = ""
            for (key,value) in self {
                output +=  "\(key)=\(value)&"
            }
            output = String(output.characters.dropLast())
            return output
        }
    }
    
    print(populatedDictionary.queryString)
    
    // Output : key1=value1&key2=value2
    

    Hope it helps. Happy Coding!!

提交回复
热议问题