<!--
{
  "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/url(relativeTo:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLComponents(im)URLRelativeToURL:"
  },
  "title" : "url(relativeTo:)"
}
-->

# url(relativeTo:)

Returns a URL object derived from the components object.

```
func url(relativeTo baseURL: URL?) -> URL?
```

## Parameters

`baseURL`

If non-`nil`, this URL is used as the base URL portion of the resulting URL object.

## Discussion

If the components object has an authority component (user, password, host, or port) and a path component, then the path must either begin with `"/"` or be an empty string. Otherwise, this property contains `nil`.

If the `NSURLComponents`*does not* have an authority component (user, password, host, or port) and has a path component, the path component must not start with `"//"`. If it does, this property contains `nil`.

To configure a components object based on an existing URL, call either the [`componentsWithURL:resolvingAgainstBaseURL:`](/documentation/Foundation/NSURLComponents/componentsWithURL:resolvingAgainstBaseURL:) or [`init(url:resolvingAgainstBaseURL:)`](/documentation/Foundation/NSURLComponents/init(url:resolvingAgainstBaseURL:)-3bbte) method.

---

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)