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

# init(string:)

Creates a URL instance from the provided string.

## Declaration

```swift
init?(string: String)
```

## Parameters

- `string`: A URL location.

## 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:encodingInvalidCharacters:)](foundation/url/init(string:encodinginvalidcharacters:).md)
- [init(string:relativeTo:)](foundation/url/init(string:relativeto:).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)
