---
title: URL
framework: foundation
role: symbol
role_heading: Structure
path: foundation/url
---

# URL

A value that identifies the location of a resource, such as an item on a remote server or the path to a local file.

## Declaration

```swift
struct URL
```

## Mentioned in

Using the file system effectively Checking Volume Storage Capacity Downloading files from websites Encoding and Decoding Custom Types Processing URL session data task results with Combine

## Overview

Overview You can construct URLs and access their parts. For URLs that represent local files, you can also manipulate properties of those files directly, such as changing the file’s last modification date. Finally, you can pass URLs to other APIs to retrieve the contents of those URLs. For example, you can use URLSession and its related classes to access the contents of remote resources. URLs are the preferred way to refer to local files. Most objects that read data from or write data to a file have methods that accept a URL instead of a pathname as the file reference. For example, you can get the contents of a local file URL as String by calling init(contentsOf:encoding:), or as a Data by calling init(contentsOf:options:). As a convenience, you can use Swift’s async-await syntax to asynchronously access the contents of a URL through the resourceBytes and lines properties. These properties use the shared URLSession instance to load the resource. URL defines a set of properties for common directories like documentsDirectory and cachesDirectory, some of which have distinct behaviors for backup or automatic purging. To make the best use of these directories, see Using the file system effectively.

## Topics

### Creating a URL from a string

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

### Creating a file URL from a string path

- [init(filePath:directoryHint:relativeTo:)](foundation/url/init(filepath:directoryhint:relativeto:).md)
- [URL.DirectoryHint](foundation/url/directoryhint.md)
- [init(fileURLWithPath:)](foundation/url/init(fileurlwithpath:).md)
- [init(fileURLWithPath:isDirectory:)](foundation/url/init(fileurlwithpath:isdirectory:).md)
- [init(fileURLWithPath:relativeTo:)](foundation/url/init(fileurlwithpath:relativeto:).md)
- [init(fileURLWithPath:isDirectory:relativeTo:)](foundation/url/init(fileurlwithpath:isdirectory:relativeto:).md)
- [init(fileURLWithFileSystemRepresentation:isDirectory:relativeTo:)](foundation/url/init(fileurlwithfilesystemrepresentation:isdirectory:relativeto:).md)
- [init(fileReferenceLiteralResourceName:)](foundation/url/init(filereferenceliteralresourcename:).md)
- [init(filePath:directoryHint:)](foundation/url/init(filepath:directoryhint:).md)

### Creating a file URL from a file path

- [init(_:)](foundation/url/init(_:).md)
- [init(_:isDirectory:)](foundation/url/init(_:isdirectory:).md)
- [FilePath](system/filepath.md)

### Creating a file URL for a common directory

- [init(for:in:appropriateFor:create:)](foundation/url/init(for:in:appropriatefor:create:).md)
- [FileManager.SearchPathDirectory](foundation/filemanager/searchpathdirectory.md)
- [FileManager.SearchPathDomainMask](foundation/filemanager/searchpathdomainmask.md)

### Creating a URL by resolving a bookmark

- [init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)](foundation/url/init(resolvingbookmarkdata:options:relativeto:bookmarkdataisstale:)-3ic6f.md)
- [init(resolvingAliasFileAt:options:)](foundation/url/init(resolvingaliasfileat:options:).md)
- [URL.BookmarkResolutionOptions](foundation/url/bookmarkresolutionoptions.md)
- [NSURL.BookmarkResolutionOptions](foundation/nsurl/bookmarkresolutionoptions.md)

### Creating a URL from a resource

- [init(resource:)](foundation/url/init(resource:).md)

### Creating a URL by parsing

- [init(_:strategy:)](foundation/url/init(_:strategy:).md)
- [URL.ParseStrategy](foundation/url/parsestrategy.md)

### Accessing the parts of a URL

- [fragment(percentEncoded:)](foundation/url/fragment(percentencoded:).md)
- [fragment](foundation/url/fragment.md)
- [host(percentEncoded:)](foundation/url/host(percentencoded:).md)
- [host](foundation/url/host.md)
- [lastPathComponent](foundation/url/lastpathcomponent.md)
- [path(percentEncoded:)](foundation/url/path(percentencoded:).md)
- [path](foundation/url/path.md)
- [password(percentEncoded:)](foundation/url/password(percentencoded:).md)
- [password](foundation/url/password.md)
- [pathComponents](foundation/url/pathcomponents.md)
- [pathExtension](foundation/url/pathextension.md)
- [port](foundation/url/port.md)
- [query(percentEncoded:)](foundation/url/query(percentencoded:).md)
- [query](foundation/url/query.md)
- [scheme](foundation/url/scheme.md)
- [user(percentEncoded:)](foundation/url/user(percentencoded:).md)
- [user](foundation/url/user.md)

### Accessing URL representations

- [baseURL](foundation/url/baseurl.md)
- [absoluteString](foundation/url/absolutestring.md)
- [absoluteURL](foundation/url/absoluteurl.md)
- [relativePath](foundation/url/relativepath.md)
- [relativeString](foundation/url/relativestring.md)
- [standardized](foundation/url/standardized.md)
- [standardizedFileURL](foundation/url/standardizedfileurl.md)

### Accessing resource values

- [resourceValues(forKeys:)](foundation/url/resourcevalues(forkeys:).md)
- [setResourceValues(_:)](foundation/url/setresourcevalues(_:).md)
- [removeCachedResourceValue(forKey:)](foundation/url/removecachedresourcevalue(forkey:).md)
- [removeAllCachedResourceValues()](foundation/url/removeallcachedresourcevalues().md)
- [setTemporaryResourceValue(_:forKey:)](foundation/url/settemporaryresourcevalue(_:forkey:).md)
- [URLResourceKey](foundation/urlresourcekey.md)
- [URLResourceValues](foundation/urlresourcevalues.md)

### Working with the data representation of a URL

- [init(dataRepresentation:relativeTo:isAbsolute:)](foundation/url/init(datarepresentation:relativeto:isabsolute:).md)
- [dataRepresentation](foundation/url/datarepresentation.md)

### Working with file URLs

- [isFileURL](foundation/url/isfileurl.md)
- [hasDirectoryPath](foundation/url/hasdirectorypath.md)
- [withUnsafeFileSystemRepresentation(_:)](foundation/url/withunsafefilesystemrepresentation(_:).md)
- [resolveSymlinksInPath()](foundation/url/resolvesymlinksinpath().md)
- [resolvingSymlinksInPath()](foundation/url/resolvingsymlinksinpath().md)
- [standardize()](foundation/url/standardize().md)

### Accessing common directories

- [applicationDirectory](foundation/url/applicationdirectory.md)
- [applicationSupportDirectory](foundation/url/applicationsupportdirectory.md)
- [cachesDirectory](foundation/url/cachesdirectory.md)
- [desktopDirectory](foundation/url/desktopdirectory.md)
- [documentsDirectory](foundation/url/documentsdirectory.md)
- [downloadsDirectory](foundation/url/downloadsdirectory.md)
- [libraryDirectory](foundation/url/librarydirectory.md)
- [moviesDirectory](foundation/url/moviesdirectory.md)
- [musicDirectory](foundation/url/musicdirectory.md)
- [picturesDirectory](foundation/url/picturesdirectory.md)
- [sharedPublicDirectory](foundation/url/sharedpublicdirectory.md)
- [temporaryDirectory](foundation/url/temporarydirectory.md)
- [trashDirectory](foundation/url/trashdirectory.md)
- [userDirectory](foundation/url/userdirectory.md)

### Accessing home and user directories

- [currentDirectory()](foundation/url/currentdirectory().md)
- [homeDirectory](foundation/url/homedirectory.md)
- [homeDirectory(forUser:)](foundation/url/homedirectory(foruser:).md)

### Adding path components

- [append(path:directoryHint:)](foundation/url/append(path:directoryhint:).md)
- [append(component:directoryHint:)](foundation/url/append(component:directoryhint:).md)
- [appendPathComponent(_:)](foundation/url/appendpathcomponent(_:).md)
- [appendPathComponent(_:isDirectory:)](foundation/url/appendpathcomponent(_:isdirectory:).md)
- [appending(path:directoryHint:)](foundation/url/appending(path:directoryhint:).md)
- [appending(component:directoryHint:)](foundation/url/appending(component:directoryhint:).md)
- [appendingPathComponent(_:)](foundation/url/appendingpathcomponent(_:).md)
- [appendingPathComponent(_:isDirectory:)](foundation/url/appendingpathcomponent(_:isdirectory:).md)
- [append(components:directoryHint:)](foundation/url/append(components:directoryhint:).md)
- [appending(components:directoryHint:)](foundation/url/appending(components:directoryhint:).md)
- [appendPathComponent(_:conformingTo:)](foundation/url/appendpathcomponent(_:conformingto:).md)
- [appendingPathComponent(_:conformingTo:)](foundation/url/appendingpathcomponent(_:conformingto:).md)

### Adding a path extension

- [appendPathExtension(_:)](foundation/url/appendpathextension(_:).md)
- [appendingPathExtension(_:)](foundation/url/appendingpathextension(_:).md)
- [appendPathExtension(for:)](foundation/url/appendpathextension(for:).md)
- [appendingPathExtension(for:)](foundation/url/appendingpathextension(for:).md)

### Adding query items

- [append(queryItems:)](foundation/url/append(queryitems:).md)
- [appending(queryItems:)](foundation/url/appending(queryitems:).md)
- [URLQueryItem](foundation/urlqueryitem.md)

### Removing path components

- [deleteLastPathComponent()](foundation/url/deletelastpathcomponent().md)
- [deletingLastPathComponent()](foundation/url/deletinglastpathcomponent().md)

### Removing a path extension

- [deletePathExtension()](foundation/url/deletepathextension().md)
- [deletingPathExtension()](foundation/url/deletingpathextension().md)

### Creating bookmarks

- [bookmarkData(options:includingResourceValuesForKeys:relativeTo:)](foundation/url/bookmarkdata(options:includingresourcevaluesforkeys:relativeto:).md)
- [bookmarkData(withContentsOf:)](foundation/url/bookmarkdata(withcontentsof:).md)
- [writeBookmarkData(_:to:)](foundation/url/writebookmarkdata(_:to:).md)
- [resourceValues(forKeys:fromBookmarkData:)](foundation/url/resourcevalues(forkeys:frombookmarkdata:).md)
- [URL.BookmarkCreationOptions](foundation/url/bookmarkcreationoptions.md)
- [NSURL.BookmarkCreationOptions](foundation/nsurl/bookmarkcreationoptions.md)

### Checking reachability

- [checkResourceIsReachable()](foundation/url/checkresourceisreachable().md)

### Loading URL contents asynchronously

- [resourceBytes](foundation/url/resourcebytes.md)
- [lines](foundation/url/lines.md)
- [URL.AsyncBytes](foundation/url/asyncbytes.md)

### Working with promised items

- [checkPromisedItemIsReachable()](foundation/url/checkpromiseditemisreachable().md)
- [promisedItemResourceValues(forKeys:)](foundation/url/promiseditemresourcevalues(forkeys:).md)

### Working with security scoped resources

- [startAccessingSecurityScopedResource()](foundation/url/startaccessingsecurityscopedresource().md)
- [stopAccessingSecurityScopedResource()](foundation/url/stopaccessingsecurityscopedresource().md)

### Describing a URL

- [customPlaygroundQuickLook](foundation/url/customplaygroundquicklook.md)

### Formatting a URL

- [formatted()](foundation/url/formatted().md)
- [formatted(_:)](foundation/url/formatted(_:).md)
- [URL.FormatStyle](foundation/url/formatstyle.md)

### Using reference types

- [NSURL](foundation/nsurl.md)

### App Intents support

- [defaultResolverSpecification](foundation/url/defaultresolverspecification.md)
- [URL.Specification](foundation/url/specification.md)
- [URL.UnwrappedType](foundation/url/unwrappedtype.md)
- [URL.ValueType](foundation/url/valuetype.md)

### Structures

- [URL.Template](foundation/url/template.md)

### Initializers

- [init(template:variables:)](foundation/url/init(template:variables:).md)

## Relationships

### Conforms To

- [CVAttachmentValueRepresentable](corevideo/cvattachmentvaluerepresentable.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [CustomURLRepresentationParameterConvertible](appintents/customurlrepresentationparameterconvertible.md)
- [Decodable](swift/decodable.md)
- [Encodable](swift/encodable.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [IntentValueConvertible](appintents/intentvalueconvertible.md)
- [IntentValueExpressing](appintents/intentvalueexpressing.md)
- [ReferenceConvertible](foundation/referenceconvertible.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
- [Transferable](coretransferable/transferable.md)

## See Also

### URLs

- [URLComponents](foundation/urlcomponents.md)
- [URLQueryItem](foundation/urlqueryitem.md)
