在Swift 3.0中将以下urlString转换为已编码的urlString

时间:2016-11-16 10:09:44

标签: swift url swift3 nsurl url-encoding

2 个答案:

答案 0 :(得分:1)

您可以像这样对URL进行编码和解码。

let stringURL = "https://maps.googleapis.com/maps/api/geocode/json?address=madhapur Hyderabad"
if let encodeURL = stringURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
    print(encodeURL)
    let decodeURL = encodeURL.removingPercentEncoding
    print(decodeURL)
}

输出

https://maps.googleapis.com/maps/api/geocode/json?address=madhapur%20Hyderabad
Optional("https://maps.googleapis.com/maps/api/geocode/json?address=madhapur Hyderabad")

答案 1 :(得分:0)

您可以使用 removedPercentEncoding

var str = "https:%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fgeocode%2Fjson%3Faddress=madhapur.%20Hyderabad"
str = str.removingPercentEncoding!

结果: - https://maps.googleapis.com/maps/api/geocode/json?address=madhapur. Hyderabad