Structure

URLComponents

A structure that parses URLs into and constructs URLs from their constituent parts.

Declaration

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

Creating URL Components

init()

Initializes with all components undefined.

init?(string: String)

Initializes from a URL string.

init?(url: URL, resolvingAgainstBaseURL: Bool)

Initializes with the components of a URL.

Getting the URL

var url: URL?

A URL created from the components.

func url(relativeTo: URL?) -> URL?

Returns a URL based on the component settings and relative to a given base URL.

var string: String?

A URL derived from the components object, in string form.

Accessing Components in Native Format

var fragment: String?

The fragment subcomponent.

var host: String?

The host subcomponent.

var password: String?

The password subcomponent of the URL.

var path: String

The path subcomponent.

var port: Int?

The port subcomponent.

var query: String?

The query subcomponent.

var queryItems: [URLQueryItem]?

An array of query items for the URL in the order in which they appear in the original query string.

var scheme: String?

The scheme subcomponent of the URL.

var user: String?

The user subcomponent of the URL.

Accessing Components in URL-Encoded Format

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: String

The path subcomponent, percent-encoded.

var percentEncodedQuery: String?

The query subcomponent, percent-encoded.

var percentEncodedUser: String?

The user subcomponent, percent-encoded.

Locating Components in the URL String Representation

var rangeOfFragment: Range<String.Index>?

Returns the character range of the fragment in the string returned by the string property.

var rangeOfHost: Range<String.Index>?

Returns the character range of the host in the string returned by the string property.

var rangeOfPassword: Range<String.Index>?

Returns the character range of the password in the string returned by the string property.

var rangeOfPath: Range<String.Index>?

Returns the character range of the path in the string returned by the string property.

var rangeOfPort: Range<String.Index>?

Returns the character range of the port in the string returned by the string property.

var rangeOfQuery: Range<String.Index>?

Returns the character range of the query in the string returned by the string property.

var rangeOfScheme: Range<String.Index>?

Returns the character range of the scheme in the string returned by the string property.

var rangeOfUser: Range<String.Index>?

Returns the character range of the user in the string returned by the string property.

Comparing URL Components

static func != (URLComponents, URLComponents) -> Bool

Returns a Boolean value indicating whether two values are not equal.

static func == (URLComponents, URLComponents) -> Bool

Indicates whether two URL component instances are the same.

Describing URL Components

var description: String

A textual description of the URL components.

var debugDescription: String

A textual description of the URL components suitable for debugging.

var customMirror: Mirror

A mirror that reflects the URL components.

var hashValue: Int

The computed hash value for the URL components.

Using Reference Types

class NSURLComponents

An object that parses URLs into and constructs URLs from their constituent parts that bridges to URLComponents; use NSURLComponents when you need reference semantics or other Foundation-specific behavior.

typealias URLComponents.ReferenceType

An alias for this value type's equivalent reference type.

Instance Methods

See Also

URLs

struct URL

A value that identifies the location of a resource, such as an item on a remote server or the path to a local file.

struct URLQueryItem

A single name-value pair from the query portion of a URL.