Instance Method


Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.


func addingPercentEncoding(withAllowedCharacters allowedCharacters: CharacterSet) -> String?



The characters not replaced in the string. Typically, you specify one of the predefined character sets for a particular URL component, such as urlPathAllowed or urlQueryAllowed.

Return Value

Returns the encoded string, or nil if the transformation is not possible.


Entire URL strings cannot be percent-encoded, because each URL component specifies a different set of allowed characters. For example, the query component of a URL allows the “@” character, but that character must be percent-encoded in the password component.

UTF-8 encoding is used to determine the correct percent-encoded characters. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.

See Also

Working with URL Strings

var removingPercentEncoding: String?

Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.