<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLComponents",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation13URLComponentsV"
  },
  "title" : "URLComponents"
}
-->

# URLComponents

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](http://www.ietf.org/rfc/rfc3986.txt). Its behavior differs subtly from that of the [`URL`](/documentation/Foundation/URL) structure, which conforms to older RFCs. However, you can easily obtain a [`URL`](/documentation/Foundation/URL) value based on the contents of a [`URLComponents`](/documentation/Foundation/URLComponents) value or vice versa.

## Topics

### Creating URL components

[`init()`](/documentation/Foundation/URLComponents/init())

Creates a URL components instance without defining any of the components.

[`init(string:)`](/documentation/Foundation/URLComponents/init(string:))

Creates a URL components instance from a URL string.

[`init(string:encodingInvalidCharacters:)`](/documentation/Foundation/URLComponents/init(string:encodingInvalidCharacters:))

Creates a URL components instance from the provided string, optionally IDNA- and percent-encoding any invalid characters.

[`init(url:resolvingAgainstBaseURL:)`](/documentation/Foundation/URLComponents/init(url:resolvingAgainstBaseURL:))

Creates a URL components instance from a URL string, optionally resolving against a base URL.

### Getting the URL

[`url`](/documentation/Foundation/URLComponents/url)

A URL created from the components.

[`url(relativeTo:)`](/documentation/Foundation/URLComponents/url(relativeTo:))

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

[`string`](/documentation/Foundation/URLComponents/string)

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

### Accessing components in native format

[`fragment`](/documentation/Foundation/URLComponents/fragment)

The fragment subcomponent.

[`host`](/documentation/Foundation/URLComponents/host)

The host subcomponent.

[`encodedHost`](/documentation/Foundation/URLComponents/encodedHost)

The host subcomponent, percent-encoded.

[`password`](/documentation/Foundation/URLComponents/password)

The password subcomponent of the URL.

[`path`](/documentation/Foundation/URLComponents/path)

The path subcomponent.

[`port`](/documentation/Foundation/URLComponents/port)

The port subcomponent.

[`query`](/documentation/Foundation/URLComponents/query)

The query subcomponent.

[`queryItems`](/documentation/Foundation/URLComponents/queryItems)

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

[`scheme`](/documentation/Foundation/URLComponents/scheme)

The scheme subcomponent of the URL.

[`user`](/documentation/Foundation/URLComponents/user)

The user subcomponent of the URL.

### Accessing components in URL-encoded format

[`percentEncodedFragment`](/documentation/Foundation/URLComponents/percentEncodedFragment)

The fragment subcomponent, percent-encoded.

[`percentEncodedHost`](/documentation/Foundation/URLComponents/percentEncodedHost)

The host subcomponent, percent-encoded.

[`percentEncodedPassword`](/documentation/Foundation/URLComponents/percentEncodedPassword)

The password subcomponent, percent-encoded.

[`percentEncodedPath`](/documentation/Foundation/URLComponents/percentEncodedPath)

The path subcomponent, percent-encoded.

[`percentEncodedQuery`](/documentation/Foundation/URLComponents/percentEncodedQuery)

The query subcomponent, percent-encoded.

[`percentEncodedQueryItems`](/documentation/Foundation/URLComponents/percentEncodedQueryItems)

The query subcomponent, as an array of percent-encoded query items.

[`URLQueryItem`](/documentation/Foundation/URLQueryItem)

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

[`percentEncodedUser`](/documentation/Foundation/URLComponents/percentEncodedUser)

The user subcomponent, percent-encoded.

### Locating components in the URL string representation

[`rangeOfFragment`](/documentation/Foundation/URLComponents/rangeOfFragment)

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

[`rangeOfHost`](/documentation/Foundation/URLComponents/rangeOfHost)

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

[`rangeOfPassword`](/documentation/Foundation/URLComponents/rangeOfPassword)

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

[`rangeOfPath`](/documentation/Foundation/URLComponents/rangeOfPath)

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

[`rangeOfPort`](/documentation/Foundation/URLComponents/rangeOfPort)

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

[`rangeOfQuery`](/documentation/Foundation/URLComponents/rangeOfQuery)

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

[`rangeOfScheme`](/documentation/Foundation/URLComponents/rangeOfScheme)

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

[`rangeOfUser`](/documentation/Foundation/URLComponents/rangeOfUser)

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

### Using reference types

[`NSURLComponents`](/documentation/Foundation/NSURLComponents)

An object that parses URLs into and constructs URLs from their constituent parts.

## Relationships

### Conforms To

[`Escapable`](/documentation/Swift/Escapable)

[`ReferenceConvertible`](/documentation/Foundation/ReferenceConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomReflectable`](/documentation/Swift/CustomReflectable)

[`Sendable`](/documentation/Swift/Sendable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Encodable`](/documentation/Swift/Encodable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Copyable`](/documentation/Swift/Copyable)

[`Hashable`](/documentation/Swift/Hashable)

[`Decodable`](/documentation/Swift/Decodable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)