<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSURL/init(string:relativeTo:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURL(im)initWithString:relativeToURL:"
  },
  "title" : "init(string:relativeTo:)"
}
-->

# init(string:relativeTo:)

Initializes an NSURL object with a base URL and a relative string.

```
init?(string URLString: String, relativeTo baseURL: URL?)
```

## Parameters

`URLString`

The URL string with which to initialize the NSURL object. Linked on or after iOS 17, this method parses `URLString` according to RFC 3986. Linked before iOS 17, this method parses `URLString` according to RFCs 1738 and 1808. `URLString` is interpreted relative to `baseURL`.

`baseURL`

The base URL for the NSURL object.

## Return Value

An NSURL object initialized with `URLString` and `baseURL`. If `URLString` was malformed, returns `nil`.

## Discussion

This method allows you to create a URL relative to a base path or URL. For example, if you have the URL for a folder on disk and the name of a file within that folder, you can construct a URL for the file by providing the folder’s URL as the base path (with a trailing slash) and the filename as the string part.

> Important:
> For apps linked on or after iOS 17 and aligned OS versions, ``doc://com.apple.foundation/documentation/Foundation/NSURL`` parsing has updated from the obsolete RFC 1738/1808 parsing to the same [RFC 3986](https://www.ietf.org/rfc/rfc3986.txt) parsing as ``doc://com.apple.foundation/documentation/Foundation/NSURLComponents``. This unifies the parsing behaviors of the `NSURL` and `NSURLComponents` APIs. Now, `NSURL` automatically percent- and IDNA-encodes invalid characters to help create a valid URL.

To check if `URLString` is strictly valid according to the RFC, use the new `[NSURL URLWithString:URLString encodingInvalidCharacters:NO]` method. This method leaves all characters as they are and returns `nil` if `URLString` is explicitly invalid.

For apps linked before iOS 17, this method expects `URLString` to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent encoded. Any percent-encoded characters are interpreted using UTF-8 encoding.

[`init(string:relativeTo:)`](/documentation/Foundation/NSURL/init(string:relativeTo:)) is the designated initializer for NSURL.

## See Also

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

An object that represents the location of a resource, such as an item on a remote server or the path to a local file.

[`+  URLWithString:relativeToURL:`](/documentation/Foundation/NSURL/URLWithString:relativeToURL:)

Creates and returns an NSURL object initialized with a base URL and a relative string.



---

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)