Structure

URL

A URL is a type that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.

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 the URLSession classes to access the contents of remote resources, as described in URL Session Programming Guide.

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 func init(contentsOf:encoding) throws, or as a Data by calling func init(contentsOf:options) throws.

Symbols

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(fileReferenceLiteralResourceName: 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. File system representation is a null-terminated C string with canonical UTF-8 encoding.

init(fileURLWithPath: String)

Initializes a newly created file URL referencing the local file or directory at path.

init(fileURLWithPath: String, isDirectory: Bool)

Initializes a newly created file URL referencing the local file or directory at path.

init(fileURLWithPath: String, isDirectory: Bool, relativeTo: URL?)

Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.

init(fileURLWithPath: String, relativeTo: URL?)

Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.

init(resolvingAliasFileAt: URL, options: URL.BookmarkResolutionOptions)

Creates and initializes an NSURL that refers to the location specified by resolving the alias file at url. If the url argument does not refer to an alias file as defined by the NSURLIsAliasFileKey property, the NSURL returned is the same as url argument. This method fails and returns nil if the url argument is unreachable, or if the original file or directory could not be located or is not reachable, or if the original file or directory is on a volume that could not be located or mounted. The URLBookmarkResolutionWithSecurityScope option is not supported by this method.

init?(string: String)

Initialize with string.

init?(string: String, relativeTo: URL?)

Initialize with string, relative to another URL.

Instance Properties

var absoluteString: String

Returns the absolute string for the URL.

var absoluteURL: URL

Returns the absolute URL.

var baseURL: URL?

Returns the base URL.

var customPlaygroundQuickLook: PlaygroundQuickLook
var dataRepresentation: Data

Returns the data representation of the URL’s relativeString.

var debugDescription: Stringvar description: String
var fragment: String?

If the URL conforms to RFC 1808 (the most common form of URL), returns the fragment component of the URL; otherwise it returns nil.

var hasDirectoryPath: Bool

Returns true if the URL path represents a directory.

var hashValue: Int
var host: String?

If the URL conforms to RFC 1808 (the most common form of URL), returns the host component of the URL; otherwise it returns nil.

var isFileURL: Bool

Returns true if the scheme is file:.

var lastPathComponent: String

Returns the last path component of the URL, or an empty string if the path is an empty string.

var password: String?

If the URL conforms to RFC 1808 (the most common form of URL), returns the password component of the URL; otherwise it returns nil.

var path: String

If the URL conforms to RFC 1808 (the most common form of URL), returns the path component of the URL; otherwise it returns an empty string.

var pathComponents: [String]

Returns the path components of the URL, or an empty array if the path is an empty string.

var pathExtension: String

Returns the path extension of the URL, or an empty string if the path is an empty string.

var port: Int?

If the URL conforms to RFC 1808 (the most common form of URL), returns the port component of the URL; otherwise it returns nil.

var query: String?

If the URL conforms to RFC 1808 (the most common form of URL), returns the query of the URL; otherwise it returns nil.

var relativePath: String

If the URL conforms to RFC 1808 (the most common form of URL), returns the relative path of the URL; otherwise it returns nil.

var relativeString: String

The relative portion of a URL.

var scheme: String?

Returns the scheme of the URL.

var standardized: URL

Returns a URL with any instances of “..” or “.” removed from its path.

var standardizedFileURL: URL

Standardizes the path of a file URL.

var user: String?

If the URL conforms to RFC 1808 (the most common form of URL), returns the user component of the URL; otherwise it returns nil.

Instance Methods

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 appendingPathComponent(String)

Returns a URL constructed by appending the given path component to self.

func appendingPathComponent(String, isDirectory: Bool)

Returns a URL constructed by appending the given path component to self.

func appendingPathExtension(String)

Returns a URL constructed by appending the given path extension to self.

func bookmarkData(options: URL.BookmarkCreationOptions, includingResourceValuesForKeys: Set<URLResourceKey>?, relativeTo: URL?)

Returns bookmark data for the URL, created with specified options and resource values.

func checkPromisedItemIsReachable()

Returns whether the promised item URL’s resource exists and is reachable.

func checkResourceIsReachable()

Returns whether the URL’s resource exists and is reachable.

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()

Returns a URL constructed by removing the last path component of self.

func deletingPathExtension()

Returns a URL constructed by removing any path extension.

func promisedItemResourceValues(forKeys: Set<URLResourceKey>)

Get resource values from URLs of ‘promised’ items.

func removeAllCachedResourceValues()

Removes all cached resource values and all temporary resource values from the URL object.

func removeCachedResourceValue(forKey: URLResourceKey)

Removes the cached resource value identified by a given resource value key from the URL object.

func resolveSymlinksInPath()

Resolves any symlinks in the path of a file URL.

func resolvingSymlinksInPath()

Resolves any symlinks in the path of a file URL.

func resourceValues(forKeys: Set<URLResourceKey>)

Return a collection of resource values identified by the given resource keys.

func setResourceValues(URLResourceValues)

Sets the resource value identified by a given resource key.

func setTemporaryResourceValue(Any, forKey: URLResourceKey)

Sets a temporary resource value on the URL object.

func standardize()

Standardizes the path of a file URL.

func startAccessingSecurityScopedResource()

Given an NSURL created by resolving a bookmark data created with security scope, make the resource referenced by the url accessible to the process. When access to this resource is no longer needed the client must call stopAccessingSecurityScopedResource. Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource (Note: this is not reference counted).

func stopAccessingSecurityScopedResource()

Revokes the access granted to the url by a prior successful call to startAccessingSecurityScopedResource.

func withUnsafeFileSystemRepresentation<ResultType>((UnsafePointer<Int8>?) -> ResultType)

Passes the URL’s path in file system representation to block.

Type Methods

static func bookmarkData(withContentsOf: URL)

Initializes and returns bookmark data derived from an alias file pointed to by a specified URL. If bookmarkFileURL refers to an alias file created prior to OS X v10.6 that contains Alias Manager information but no bookmark data, this method synthesizes bookmark data for the file.

static func resourceValues(forKeys: Set<URLResourceKey>, fromBookmarkData: Data)

Returns the resource values for properties identified by a specified array of keys contained in specified bookmark data. If the result dictionary does not contain a resource value for one or more of the requested resource keys, it means those resource properties are not available in the bookmark data.

static func writeBookmarkData(Data, to: URL)

Creates an alias file on disk at a specified location with specified bookmark data. bookmarkData must have been created with the URLBookmarkCreationSuitableForBookmarkFile option. bookmarkFileURL must either refer to an existing file (which will be overwritten), or to location in an existing directory.

Operator Functions