<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSURLComponents",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLComponents"
  },
  "title" : "NSURLComponents"
}
-->

# NSURLComponents

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

```
class NSURLComponents
```

## Overview

In Swift, this object bridges to [`URLComponents`](/documentation/Foundation/URLComponents); use [`NSURLComponents`](/documentation/Foundation/NSURLComponents) when you need reference semantics or other Foundation-specific behavior.

The [`NSURLComponents`](/documentation/Foundation/NSURLComponents) class is a class that is designed to parse URLs based on [RFC 3986](http://www.ietf.org/rfc/rfc3986.txt) and to construct URLs from their constituent parts. Its behavior differs subtly from the [`NSURL`](/documentation/Foundation/NSURL) class, which conforms to older RFCs. However, you can easily obtain an [`NSURL`](/documentation/Foundation/NSURL) object based on the contents of a URL components object or vice versa.

You create a URL components object in one of three ways: from an [`NSString`](/documentation/Foundation/NSString) object that contains a URL, from an [`NSURL`](/documentation/Foundation/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.

> Important:
> The Swift overlay to the Foundation framework provides the ``doc://com.apple.foundation/documentation/Foundation/URLComponents`` structure, which bridges to the ``doc://com.apple.foundation/documentation/Foundation/NSURLComponents`` class. For more information about value types, see <doc://com.apple.documentation/documentation/Swift/working-with-foundation-types>.

## Topics

### Creating URL components

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

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

[`componentsWithString:encodingInvalidCharacters:`](/documentation/Foundation/NSURLComponents/componentsWithString:encodingInvalidCharacters:)

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

[`componentsWithURL:resolvingAgainstBaseURL:`](/documentation/Foundation/NSURLComponents/componentsWithURL:resolvingAgainstBaseURL:)

Returns a URL components object by parsing the URL from an `NSURL` object.

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

Creates a URL components object with all components left undefined.

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

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

[`init(string:encodingInvalidCharacters:)`](/documentation/Foundation/NSURLComponents/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/NSURLComponents/init(url:resolvingAgainstBaseURL:)-3bbte)

Creates a URL components object by parsing the URL from an `NSURL` object.

### Getting the URL

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

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

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

A URL object derived from the components object.

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

Returns a URL object derived from the components object.

### Accessing components in native format

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

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

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

The host URL subcomponent, or nil if not present.

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

The host subcomponent, percent-encoded.

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

The password URL subcomponent, or nil if not present.

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

The path URL component, or nil if not present.

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

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

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

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

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

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

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

The scheme URL component, or nil if not present.

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

The username URL subcomponent, or nil if not present.

### Accessing components in URL-encoded format

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

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

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

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

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

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

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

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

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

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

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

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

### Locating components in the URL string representation

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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



---

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)