Class

NSURLComponents

The NSURLComponents class is a class that is designed to parse URLs based on RFC 3986 and to construct URLs from their constituent parts. Its behavior differs subtly from the NSURL class, which conforms to older RFCs. However, you can easily obtain an NSURL object based on the contents of a URL components object or vice versa.

Overview

You create a URL components object in one of three ways: from an NSString object that contains a URL, from an NSURL object, or from scratch by using the default initializer. From there, you can modify the URL’s individual components and subcomponents by modifying various properties, either in unencoded form or in URL-encoded form. If you set the unencoded property, you can then obtain the encoded equivalent by reading the encoded property value and vice versa.

Symbols

Creating and Configuring

init()

Initializes a URL components object with nil for every component.

init?(string: String)

Initializes a URL components object by parsing a URL in string form.

init?(url: URL, resolvingAgainstBaseURL: Bool)

Initializes a URL components object by parsing the URL from an NSURL object.

Obtaining a URL

var string: String?

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

var url: URL?

A URL object derived from the components object.

func url(relativeTo: URL?)

Returns a URL object derived from the components object.

Properties in Native Format

var fragment: String?

The fragment URL component (the part after a # symbol), or nil if not present.

var host: String?

The host URL subcomponent, or nil if not present.

var password: String?

The password URL subcomponent, or nil if not present.

var path: String?

The path URL component, or nil if not present.

var port: NSNumber?

The port number URL component, or nil if not present.

var query: String?

The query URL component as a string, or nil if not present.

var queryItems: [URLQueryItem]?

The query URL component as an array of name/value pairs.

var scheme: String?

The scheme URL component, or nil if not present.

var user: String?

The username URL subcomponent, or nil if not present.

Properties in URL-Encoded Format

var percentEncodedFragment: String?

The fragment URL component (the part after a # symbol) expressed as a URL-encoded string, or nil if not present.

var percentEncodedHost: String?

The host URL subcomponent expressed as a URL-encoded string, or nil if not present.

var percentEncodedPassword: String?

The password URL subcomponent expressed as a URL-encoded string, or nil if not present.

var percentEncodedPath: String?

The path URL component expressed as a URL-encoded string, or nil if not present.

var percentEncodedQuery: String?

The query URL component expressed as a URL-encoded string, or nil if not present.

var percentEncodedUser: String?

The username URL subcomponent expressed as a URL-encoded string, or nil if not present.

Relationships

Inherits From