---
title: "url(relativeTo:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlcomponents/url(relativeto:)"
---

# url(relativeTo:)

Returns a URL based on the component settings and relative to a given base URL.

## Declaration

```swift
func url(relativeTo base: URL?) -> URL?
```

## Discussion

Discussion If the NSURLComponents 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. 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 those requirements are not met, nil is returned.

## See Also

### Getting the URL

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