---
title: NSURLComponents
framework: foundation
role: symbol
role_heading: Class
path: foundation/nsurlcomponents
---

# NSURLComponents

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

## Declaration

```swift
class NSURLComponents
```

## Overview

Overview In Swift, this object bridges to URLComponents; use NSURLComponents when you need reference semantics or other Foundation-specific behavior. 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. 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. important: The Swift overlay to the Foundation framework provides the URLComponents structure, which bridges to the NSURLComponents class. For more information about value types, see Working with Foundation Types.

## Topics

### Creating URL components

- [init()](foundation/nsurlcomponents/init().md)
- [init(string:)](foundation/nsurlcomponents/init(string:).md)
- [init(string:encodingInvalidCharacters:)](foundation/nsurlcomponents/init(string:encodinginvalidcharacters:).md)
- [init(url:resolvingAgainstBaseURL:)](foundation/nsurlcomponents/init(url:resolvingagainstbaseurl:)-3bbte.md)

### Getting the URL

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

### Accessing components in native format

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

### Accessing components in URL-encoded format

- [percentEncodedFragment](foundation/nsurlcomponents/percentencodedfragment.md)
- [percentEncodedHost](foundation/nsurlcomponents/percentencodedhost.md)
- [percentEncodedPassword](foundation/nsurlcomponents/percentencodedpassword.md)
- [percentEncodedPath](foundation/nsurlcomponents/percentencodedpath.md)
- [percentEncodedQuery](foundation/nsurlcomponents/percentencodedquery.md)
- [percentEncodedUser](foundation/nsurlcomponents/percentencodeduser.md)

### Locating components in the URL string representation

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

### Initializers

- [init(URL:resolvingAgainstBaseURL:)](foundation/nsurlcomponents/init(url:resolvingagainstbaseurl:)-5k5ld.md)
- [init(URL:resolvingAgainstBaseURL:)](foundation/nsurlcomponents/init(url:resolvingagainstbaseurl:)-6mpr5.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
