Swift3 汉字转网址 - urlencode

111 阅读1分钟

在搜索信息的时候,需要用到汉字输入,如果不将汉字转码将会导致无法页面或者导致请求失败。

问题如下:

Alamofire`protocol witness table for Swift.String

汉字转网址
swift中汉字转为百分号的网址的转换

let keyword = "天"
print(keyword.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))

解码转汉字

let keyword = "%E5%A4%A9"
print(keyword.removingPercentEncoding)