Structureswift 6.0.1FoundationEssentials
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.
- iOS
- 8.0+
- macOS
- 10.10+
- tvOS
- 9.0+
- watchOS
- 2.0+
struct URL
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
.
Citizens in FoundationEssentials
Conformances
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Decodable
A type that can decode itself from an external representation.
protocol Encodable
A type that can encode itself to an external representation.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Sendable
Types
Type members
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?
) Initializes a newly created file URL referencing the local file or directory at path, relative to a base URL.
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(from: any Decoder
) throws init?(string: String
) Initialize with string.
init?(string: String, encodingInvalidCharacters: Bool
) Initialize with a URL string and the option to add (or skip) IDNA- and percent-encoding of invalid characters.
init?(string: String, relativeTo: URL?
) Initialize with string, relative to another URL.
static var homeDirectory: URL
The home directory for the current user (~/). Complexity: O(1)
static var temporaryDirectory: URL
The temporary directory for the current user. Complexity: O(1)
static func currentDirectory(
) -> URL The working directory of the current process. Calling this property will issue a
getcwd
syscall.static func homeDirectory(forUser: String
) -> URL? Returns the home directory for the specified user.
static func == (lhs: URL, rhs: URL
) -> Bool
Instance members
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 dataRepresentation: Data
Returns the data representation of the URL’s relativeString.
var debugDescription: String
var description: String
var fragment: String?
Returns the fragment component of the URL if present, otherwise returns
nil
.var hasDirectoryPath: Bool
Returns true if the URL path represents a directory.
var host: String?
Returns the host component of the URL if present, otherwise 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?
Returns the password component of the URL if present, otherwise returns
nil
.var path: String
Returns the path component of the URL if present, otherwise 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?
Returns the port component of the URL if present, otherwise returns
nil
.var query: String?
Returns the query component of the URL if present, otherwise returns
nil
.var relativePath: String
Returns the relative path of the URL if present, otherwise returns an empty string. This is the same as
path
ifbaseURL
isnil
.var relativeString: String
Returns 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?
Returns the user component of the URL if present, otherwise returns
nil
.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 varidic list of path components to the URL.
func append<S>(path: S, directoryHint: URL.DirectoryHint
) Appends a path to the receiver.
func append(queryItems: [URLQueryItem]
) Appends a list of
URLQueryItem
to 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
) -> URL Returns 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
) -> URL Returns a URL constructed by appending the given varidic list of path components to self.
func appending<S>(path: S, directoryHint: URL.DirectoryHint
) -> URL Returns a URL constructed by appending the given path to self.
func appending(queryItems: [URLQueryItem]
) -> URL Returns a URL constructed by appending the given list of
URLQueryItem
to self.func appendingPathComponent(String
) -> URL Returns a URL constructed by appending the given path component to self.
func appendingPathComponent(String, isDirectory: Bool
) -> URL Returns a URL constructed by appending the given path component to self.
func appendingPathExtension(String
) -> URL Returns 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(
) -> URL Returns a URL constructed by removing the last path component of self.
func deletingPathExtension(
) -> URL Returns a URL constructed by removing any path extension.
func encode(to: any Encoder
) throws func fragment(percentEncoded: Bool
) -> String? Returns the password component of the URL if present, otherwise returns
nil
.func hash(into: inout Hasher
) func host(percentEncoded: Bool
) -> String? Returns the host component of the URL if present, otherwise returns
nil
.func password(percentEncoded: Bool
) -> String? Returns the password component of the URL if present, otherwise returns
nil
.func path(percentEncoded: Bool
) -> String Returns the path component of the URL if present, otherwise returns an empty string.
func query(percentEncoded: Bool
) -> String? Returns the password component of the URL if present, otherwise returns
nil
.func resolveSymlinksInPath(
) Resolves any symlinks in the path of a file URL.
func resolvingSymlinksInPath(
) -> URL Resolves any symlinks in the path of a file URL.
func standardize(
) Standardizes the path of a file URL by removing dot segments.
func user(percentEncoded: Bool
) -> String? Returns the user component of the URL if present, otherwise returns
nil
.func withUnsafeFileSystemRepresentation<ResultType>((UnsafePointer<Int8>?) throws -> ResultType
) rethrows -> ResultType Passes the URL’s path in file system representation to
block
.
Type features
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.
Available in Foundation
Conformances
protocol CustomPlaygroundDisplayConvertible
A type that supplies a custom description for playground logging.
protocol ReferenceConvertible
Decorates types which are backed by a Foundation reference type.
Typealiases
Type members
Instance members
var playgroundDescription: Any
func checkResourceIsReachable(
) throws -> Bool Returns whether the URL’s resource exists and is reachable.
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 resourceValues(forKeys: Set<URLResourceKey>
) throws -> URLResourceValues Return a collection of resource values identified by the given resource keys.
func setResourceValues(URLResourceValues
) throws Sets the resource value identified by a given resource key.
func setTemporaryResourceValue(Any, forKey: URLResourceKey
) Sets a temporary resource value on the URL object.
Extension in AsyncHTTPClient
Type members
init?(httpURLWithSocketPath: String, uri: String
) Initializes a newly created HTTP URL connecting to a unix domain socket path. The socket path is encoded as the URL’s host, replacing percent encoding invalid path characters, and will use the “http+unix” scheme.
init?(httpsURLWithSocketPath: String, uri: String
) Initializes a newly created HTTPS URL connecting to a unix domain socket path over TLS. The socket path is encoded as the URL’s host, replacing percent encoding invalid path characters, and will use the “https+unix” scheme.