Structure
URLComponents
iOS 8.0+
iPadOS 8.0+
Mac Catalyst 8.0+
macOS 10.10+
tvOS 9.0+
watchOS 2.0+
A structure that parses URLs into and constructs URLs from their constituent parts.
struct URLComponents
Overview
This structure parses and constructs URLs according to RFC 3986. Its behavior differs subtly from that of the URL structure, which conforms to older RFCs. However, you can easily obtain a URL value based on the contents of a URLComponents value or vice versa.
Topics
Operators
Initializers
init()Initializes empty URLComponents.init?(string: String)Initializes URLComponents from a URL string.init?(string: String, encodingInvalidCharacters: Bool)Initializes URLComponents from a URL string, with option to encode invalid characters.init?(url: URL, resolvingAgainstBaseURL: Bool)Initializes URLComponents from a URL, optionally resolving against its base URL.
Instance Properties
var encodedHost: String?The host subcomponent, percent-encoded.var fragment: String?The fragment subcomponent.var host: String?The host subcomponent.var password: String?The password subcomponent.var path: StringThe path subcomponent.var percentEncodedFragment: String?The fragment subcomponent, percent-encoded.var percentEncodedHost: String?The host subcomponent, percent-encoded.var percentEncodedPassword: String?The password subcomponent, percent-encoded.var percentEncodedPath: StringThe path subcomponent, percent-encoded.var percentEncodedQuery: String?The query subcomponent, percent-encoded.var percentEncodedQueryItems: [URLQueryItem]?The query subcomponent as an array of percent-encoded URLQueryItems.var percentEncodedUser: String?The user subcomponent, percent-encoded.var port: Int?The port subcomponent.var query: String?The query subcomponent.var queryItems: [URLQueryItem]?The query subcomponent as an array of URLQueryItems.var rangeOfFragment: Range<String.Index>?The character range of the fragment in thestringproperty.var rangeOfHost: Range<String.Index>?The character range of the host in thestringproperty.var rangeOfPassword: Range<String.Index>?The character range of the password in thestringproperty.var rangeOfPath: Range<String.Index>?The character range of the path in thestringproperty.var rangeOfPort: Range<String.Index>?The character range of the port in thestringproperty.var rangeOfQuery: Range<String.Index>?The character range of the query in thestringproperty.var rangeOfScheme: Range<String.Index>?The character range of the scheme in thestringproperty.var rangeOfUser: Range<String.Index>?The character range of the user in thestringproperty.var scheme: String?The scheme subcomponent.var string: String?A URL string derived from the components.var url: URL?A URL created from the components.var user: String?The user subcomponent.
Instance Methods
func hash(into: inout Hasher)func url(relativeTo: URL?) -> URL?Returns a URL based on the components relative to a base URL.
Default Implementations
Relationships
Conforms To
Foundation.ReferenceConvertibleSwift.CopyableSwift.CustomDebugStringConvertibleSwift.CustomReflectableSwift.CustomStringConvertibleSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatype