Skip to content

Initializer

init(string:encodingInvalidCharacters:)

iOS 17.0+ iPadOS 17.0+ Mac Catalyst 17.0+ macOS 14.0+ tvOS 17.0+ watchOS 10.0+
Initializes URLComponents from a URL string, with option to encode invalid characters.
init?(string: String, encodingInvalidCharacters: Bool)

Parameters

string

The URL string.

encodingInvalidCharacters

True if URLComponents should try to encode an invalid URL string, false otherwise.

Return Value

A URLComponents struct for a valid URL, or nil if the URL is invalid.

Discussion

If encodingInvalidCharacters is false, and the URL string is invalid according to RFC 3986, nil is returned. If encodingInvalidCharacters is true, URLComponents will try to encode the string to create a valid URL. If the URL string is still invalid after encoding, nil is returned.