public func addingPercentEncoding(withAllowedCharacters allowedCharacters: CharacterSet) -> String?
例如urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
CharacterSet
CharacterSet略为复杂
提供一些现成的class var变量:
class var urlQueryAllowed
class var urlFragmentAllowed: CharacterSet
class var urlHostAllowed: CharacterSet
class var urlPasswordAllowed: CharacterSet
class var urlPathAllowed: CharacterSet
class var urlUserAllowed: CharacterSet
urlQueryAllowed
: url的query component指的是’?‘后面的参数,例如: http://www.example.com/index.php?key1=value1#jump
这里指的就是key1=value1
urlFragmentAllowed
: 指的是#
后面的。例如http://www.example.com/index.html#jumpLocation
指的是jumpLocation
.
urlHostAllowed
:URL的宿主组件通常是紧接在前两个斜杠之后的组件。 如果URL包含用户名和密码,则主机组件是@符号后面的组件。 例如,在URL http:// username:password@www.example.com/index.html
中,主机组件是www.example.com。
urlUserAllowed
urlPasswordAllowed
urlPathAllowed
: 例如http://www.example.com/index.php?key1=value1
中的/index.php