---
title: URLComponents
framework: foundation
role: symbol
role_heading: Structure
path: foundation/urlcomponents
---

# URLComponents

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

## Declaration

```swift
struct URLComponents
```

## Overview

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()](foundation/urlcomponents/init().md)
- [init(string:)](foundation/urlcomponents/init(string:).md)
- [init(string:encodingInvalidCharacters:)](foundation/urlcomponents/init(string:encodinginvalidcharacters:).md)
- [init(url:resolvingAgainstBaseURL:)](foundation/urlcomponents/init(url:resolvingagainstbaseurl:).md)

### Getting the URL

- [url](foundation/urlcomponents/url.md)
- [url(relativeTo:)](foundation/urlcomponents/url(relativeto:).md)
- [string](foundation/urlcomponents/string.md)

### Accessing components in native format

- [fragment](foundation/urlcomponents/fragment.md)
- [host](foundation/urlcomponents/host.md)
- [encodedHost](foundation/urlcomponents/encodedhost.md)
- [password](foundation/urlcomponents/password.md)
- [path](foundation/urlcomponents/path.md)
- [port](foundation/urlcomponents/port.md)
- [query](foundation/urlcomponents/query.md)
- [queryItems](foundation/urlcomponents/queryitems.md)
- [scheme](foundation/urlcomponents/scheme.md)
- [user](foundation/urlcomponents/user.md)

### Accessing components in URL-encoded format

- [percentEncodedFragment](foundation/urlcomponents/percentencodedfragment.md)
- [percentEncodedHost](foundation/urlcomponents/percentencodedhost.md)
- [percentEncodedPassword](foundation/urlcomponents/percentencodedpassword.md)
- [percentEncodedPath](foundation/urlcomponents/percentencodedpath.md)
- [percentEncodedQuery](foundation/urlcomponents/percentencodedquery.md)
- [percentEncodedQueryItems](foundation/urlcomponents/percentencodedqueryitems.md)
- [URLQueryItem](foundation/urlqueryitem.md)
- [percentEncodedUser](foundation/urlcomponents/percentencodeduser.md)

### Locating components in the URL string representation

- [rangeOfFragment](foundation/urlcomponents/rangeoffragment.md)
- [rangeOfHost](foundation/urlcomponents/rangeofhost.md)
- [rangeOfPassword](foundation/urlcomponents/rangeofpassword.md)
- [rangeOfPath](foundation/urlcomponents/rangeofpath.md)
- [rangeOfPort](foundation/urlcomponents/rangeofport.md)
- [rangeOfQuery](foundation/urlcomponents/rangeofquery.md)
- [rangeOfScheme](foundation/urlcomponents/rangeofscheme.md)
- [rangeOfUser](foundation/urlcomponents/rangeofuser.md)

### Using reference types

- [NSURLComponents](foundation/nsurlcomponents.md)

## Relationships

### Conforms To

- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomReflectable](swift/customreflectable.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Decodable](swift/decodable.md)
- [Encodable](swift/encodable.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [ReferenceConvertible](foundation/referenceconvertible.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### URLs

- [URL](foundation/url.md)
- [URLQueryItem](foundation/urlqueryitem.md)
