---
title: "init(string:relativeTo:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/url/init(string:relativeto:)"
---

# init(string:relativeTo:)

Creates a URL instance from the provided string, relative to another URL.

## Declaration

```swift
init?(string: String, relativeTo url: URL?)
```

## Parameters

- `string`: A relative URL location.
- `url`: A URL that provides a base location that the string extends.

## Discussion

Discussion important: For apps linked on or after iOS 17 and aligned OS versions, URL parsing has updated from the obsolete RFC 1738/1808 parsing to the same RFC 3986 parsing as URLComponents. This unifies the parsing behaviors of the URL and URLComponents APIs. Now, URL automatically percent- and IDNA-encodes invalid characters to help create a valid URL. This initializer returns nil if the string doesn’t represent a valid URL even after encoding invalid characters. To check if a URL string is strictly valid according to the RFC, use the new init(string:encodingInvalidCharacters:) initializer and pass encodingInvalidCharacters: false. This leaves all characters as they are and returns nil if the URL string is explicitly invalid.

## See Also

### Creating a URL from a string

- [init(string:)](foundation/url/init(string:).md)
- [init(string:encodingInvalidCharacters:)](foundation/url/init(string:encodinginvalidcharacters:).md)
- [init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)](foundation/url/init(resolvingbookmarkdata:options:relativeto:bookmarkdataisstale:)-3ic6f.md)
- [init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)](foundation/url/init(resolvingbookmarkdata:options:relativeto:bookmarkdataisstale:)-97e6x.md)
