Extensions on
Collection
Methods
percentEncodedString(using:)
@inlinable
public func percentEncodedString<EncodeSet: PercentEncodeSet>(using encodeSet: EncodeSet) -> String
Returns an ASCII string formed by percent-encoding this collection's elements.
Percent-encoding transforms arbitrary bytes to ASCII strings (e.g. the byte value 200, or 0xC8, to the string "%C8"),
with bytes within the ASCII range being restricted by encodeSet
.
Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions;
provide the appropriate SubstitutionMap
when decoding form-encoded data to accurately recover the source contents.
// Encode arbitrary data as an ASCII string.
let image: Data = ...
image.percentEncodedString(using: .urlComponentSet) // "%BAt_%E0%11%22%EB%10%2C%7F..."
// Encode-sets determine which characters are encoded, and some perform substitutions.
let bytes = "hello, world!".utf8
bytes.percentEncodedString(using: .urlComponentSet) == "hello%2C%20world!" // ✅
bytes.percentEncodedString(using: .formEncoding) == "hello%2C+world%21" // ✅
percentEncodedString(using:)
@inlinable @_disfavoredOverload
public func percentEncodedString<EncodeSet: PercentEncodeSet>(using encodeSet: EncodeSet._Member) -> String
Returns an ASCII string formed by percent-encoding this collection's elements.
Percent-encoding transforms arbitrary bytes to ASCII strings (e.g. the byte value 200, or 0xC8, to the string "%C8"),
with bytes within the ASCII range being restricted by encodeSet
.
Form-encoding (as used by HTML forms) is a legacy variant of percent-encoding which includes substitutions;
provide the appropriate SubstitutionMap
when decoding form-encoded data to accurately recover the source contents.
// Encode arbitrary data as an ASCII string.
let image: Data = ...
image.percentEncodedString(using: .urlComponentSet) // "%BAt_%E0%11%22%EB%10%2C%7F..."
// Encode-sets determine which characters are encoded, and some perform substitutions.
let bytes = "hello, world!".utf8
bytes.percentEncodedString(using: .urlComponentSet) == "hello%2C%20world!" // ✅
bytes.percentEncodedString(using: .formEncoding) == "hello%2C+world%21" // ✅