Structure
URL
struct URL
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.
Topics
Structures
struct TemplateA template for constructing a URL from variable expansions.
Operators
Initializers
init?(dataRepresentation: Data, relativeTo: URL?, isAbsolute: Bool)Initializes a newly created URL using the contents of the given data, relative to a base URL.init(filePath: String, directoryHint: URL.DirectoryHint, relativeTo: URL?)Creates a file URL that references a path you specify as a string.init(fileURLWithFileSystemRepresentation: UnsafePointer<Int8>, isDirectory: Bool, relativeTo: URL?)Initializes a newly created URL referencing the local file or directory at the file system representation of the path.init(fileURLWithPath: String)Creates a file URL that references the local file or directory at the given path.init(fileURLWithPath: String, isDirectory: Bool)Creates a file URL that references the local file or directory at the given path.init(fileURLWithPath: String, isDirectory: Bool, relativeTo: URL?)Creates a file URL that references the local file or directory at the given path, relative to a base URL.init(fileURLWithPath: String, relativeTo: URL?)Creates a file URL that references the local file or directory at the given path, relative to a base URL.init?(string: String)Creates a URL instance from the provided string.init?(string: String, encodingInvalidCharacters: Bool)Creates a URL instance from the provided string, optionally IDNA- and percent-encoding any invalid characters.init?(string: String, relativeTo: URL?)Creates a URL instance from the provided string, relative to another URL.init?(template: URL.Template, variables: [URL.Template.VariableName : URL.Template.Value])Creates a newURLby expanding the RFC 6570 template and variables.
Instance Properties
var absoluteString: StringThe absolute string for the URL.var absoluteURL: URLThe absolute URL.var baseURL: URL?The base URL.var dataRepresentation: DataThe data representation of the URL’s relativeString.var fragment: String?The fragment component of the URL if the URL conforms to RFC 3986; otherwise, nil.var hasDirectoryPath: BoolA Boolean that is true if the URL path represents a directory.var host: String?The host component of a URL if the URL conforms to RFC 3986; otherwise, nil.var isFileURL: BoolA Boolean that is true if the scheme isfile:.var lastPathComponent: StringThe last path component of the URL, or an empty string if the path is an empty string.var password: String?The password component of the URL if the URL conforms to RFC 3986; otherwise, nil.var path: StringThe path component of the URL if the URL conforms to RFC 3986; otherwise, an empty string.var pathComponents: [String]The path components of the URL, or an empty array if the path is an empty string.var pathExtension: StringThe path extension of the URL, or an empty string if the path is an empty string.var port: Int?The port component of the URL if the URL conforms to RFC 3986; otherwise, nil.var query: String?The query of the URL if the URL conforms to RFC 3986; otherwise, nil.var relativePath: StringThe relative path of the URL if the URL conforms to RFC 3986, otherwise nil.var relativeString: StringThe relative portion of a URL.var scheme: String?The scheme of the URL.var standardized: URLA version of the URL with any instances of “..” or “.” resolved in its path.var standardizedFileURL: URLA standardized version of the path of a file URL.var user: String?The user component of the URL if the URL conforms to RFC 3986; otherwise, nil.
Instance Methods
func append<S>(component: S, directoryHint: URL.DirectoryHint)Appends a path component to the receiver. The path component is first percent-encoded before being appended to the receiver.func append<S>(components: S..., directoryHint: URL.DirectoryHint)Appends a variadic list of path components to the URL.func append<S>(path: S, directoryHint: URL.DirectoryHint)Appends a path to the URL, with a hint for handling directory awareness.func append(queryItems: [URLQueryItem])Appends a list ofURLQueryItemto the receiver.func appendPathComponent(String)Appends a path component to the URL.func appendPathComponent(String, isDirectory: Bool)Appends a path component to the URL.func appendPathExtension(String)Appends the given path extension to self.func appending<S>(component: S, directoryHint: URL.DirectoryHint) -> URLReturns a URL constructed by appending the given path component to self. The path component is first percent-encoded before being appended to the receiver.func appending<S>(components: S..., directoryHint: URL.DirectoryHint) -> URLReturns a URL constructed by appending the given variadic list of path components to self.func appending<S>(path: S, directoryHint: URL.DirectoryHint) -> URLReturns a URL constructed by appending the given path to self.func appending(queryItems: [URLQueryItem]) -> URLReturns a URL constructed by appending the given list ofURLQueryItemto self.func appendingPathComponent(String) -> URLReturns a URL constructed by appending the given path component to self.func appendingPathComponent(String, isDirectory: Bool) -> URLReturns a URL constructed by appending the given path component to self.func appendingPathExtension(String) -> URLReturns a URL constructed by appending the given path extension to self.func deleteLastPathComponent()Returns a URL constructed by removing the last path component of self.func deletePathExtension()Returns a URL constructed by removing any path extension.func deletingLastPathComponent() -> URLReturns a URL constructed by removing the last path component of self.func deletingPathExtension() -> URLReturns a URL constructed by removing any path extension.func fragment(percentEncoded: Bool) -> String?Returns the fragment component of the URL, optionally removing any percent-encoding.func hash(into: inout Hasher)func host(percentEncoded: Bool) -> String?Returns the host component of the URL, optionally removing any percent-encoding.func password(percentEncoded: Bool) -> String?Returns the password component of the URL, optionally removing any percent-encoding.func path(percentEncoded: Bool) -> StringReturns the path component of the URL, optionally removing any percent-encoding.func query(percentEncoded: Bool) -> String?Returns the query component of the URL, optionally removing any percent-encoding.func resolveSymlinksInPath()Resolves any symlinks in the path of a file URL.func resolvingSymlinksInPath() -> URLResolves any symlinks in the path of a file URL.func standardize()Standardizes the path of a file URL.func user(percentEncoded: Bool) -> String?Returns the user component of the URL, optionally removing any percent-encoding.func withUnsafeFileSystemRepresentation<ResultType>((UnsafePointer<Int8>?) throws -> ResultType) rethrows -> ResultTypePasses the URL’s path in file system representation toblock.
Type Properties
static var homeDirectory: URLThe home directory for the current user.static var temporaryDirectory: URLThe standard directory for temporary files.
Type Methods
static func currentDirectory() -> URLThe working directory of the current process. Calling this property will issue agetcwdsyscall.static func homeDirectory(forUser: String) -> URL?Returns the home directory for the specified user.
Enumerations
enum DirectoryHintA hint to URL file APIs for handling paths that may reference directories.
Default Implementations
Relationships
Conforms To
Configuration.ExpressibleByConfigStringFoundation.ReferenceConvertibleMultipartKit.MultipartPartConvertibleSwift.CopyableSwift.CustomDebugStringConvertibleSwift.CustomPlaygroundDisplayConvertibleSwift.CustomStringConvertibleSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatype