Initializer
init(scheme:userinfo:host:port:path:query:fragment:)
URI from various subcomponents.init(scheme: Scheme = .init(), userinfo: String?, host: String? = nil, port: Int? = nil, path: String, query: String? = nil, fragment: String? = nil)
Discussion
Percent encoding is added to each component (if necessary) automatically. There is currently no way to change this behavior; use URLComponents instead if this is insufficient.
Important
For backwards compatibility reasons, if the path component is specified in isolation (e.g. all other components are nil), the path is parsed as if by the init(string:) initializer, EXCEPT that if the path begins with //, it will be treated as beginning with / instead, thus parsing the first path component as part of the path rather than as a host component. These semantics are suitable for parsing URI-like strings which are known to lack an authority component, such as the URI part of the first line of an HTTP request.
In all cases, a / is prepended to the path if it does not begin with one, irrespective of whether or not the path has been specified in isolation as described above.