Path
public struct Path : Pathish
extension Path: Codable
extension Path: CustomStringConvertible
extension Path: CustomDebugStringConvertible
A Path
represents an absolute path on a filesystem.
All functions on Path
are chainable and short to facilitate doing sequences
of file operations in a concise manner.
Path
supports Codable
, and can be configured to
encode paths relatively.
Sorting a Sequence
of paths will return the locale-aware sort order, which
will give you the same order as Finder.
Converting from a String
is a common first step, here are the recommended
ways to do that:
let p1 = Path.root/pathString
let p2 = Path.root/url.path
let p3 = Path.cwd/relativePathString
let p4 = Path(userInput) ?? Path.cwd/userInput
If you are constructing paths from static-strings we provide support for dynamic members:
let p1 = Path.root.usr.bin.ls // => /usr/bin/ls
However we only provide this support off of the static members like root
due
to the anti-pattern where Path.swift suddenly feels like Javascript otherwise.
Note
APath
does not necessarily represent an actual filesystem entry.
-
The normalized string representation of the underlying filesystem path
Declaration
Swift
public let string: String
-
Creates a new absolute, standardized path.
Note
Resolves any .. or . components.Note
Removes multiple subsequent and trailing occurences of/
.Note
Does not resolve any symlinks.Note
On macOS, removes an initial component of “/private/var/automount”, “/var/automount”, or “/private” from the path, if the result still indicates an existing file or directory (checked by consulting the file system).Declaration
Swift
public init?<S>(_ description: S) where S : StringProtocol
Return Value
The path or
nil
if fed a relative path or a~foo
string where there is no userfoo
. -
Creates a new absolute, standardized path from the provided file-scheme URL.
Note
If the URL is not a file URL, returnsnil
.Declaration
Swift
public init?(url: URL)
-
Creates a new absolute, standardized path from the provided file-scheme URL.
Note
If the URL is not a file URL, returnsnil
.Note
If the URL is a file reference URL, converts it to a POSIX path first.Declaration
Swift
public init?(url: NSURL)
-
A filesystem entry’s kind, file, directory, symlink etc.
See moreDeclaration
Swift
enum EntryType : CaseIterable
-
Returns a
Path
containingFileManager.default.currentDirectoryPath
.Declaration
Swift
public static var cwd: DynamicPath { get }
-
Returns a
Path
representing the root path.Declaration
Swift
public static var root: DynamicPath { get }
-
-
Undocumented
Declaration
Swift
public static func source(for filePath: String = #file) -> (file: DynamicPath, directory: DynamicPath)
-
Returns a
Path
representing the user’s home directoryDeclaration
Swift
public static var home: DynamicPath { get }
-
The root for user documents.
Note
There is no standard location for documents on Linux, thus we return~/Documents
.Note
You should create a subdirectory before creating any files.Declaration
Swift
public static var documents: DynamicPath { get }
-
The root for cache files.
Note
On Linux this isXDG_CACHE_HOME
.Note
You should create a subdirectory before creating any files.Declaration
Swift
public static var caches: DynamicPath { get }
-
For data that supports your running application.
Note
On Linux isXDG_DATA_HOME
.Note
You should create a subdirectory before creating any files.Declaration
Swift
public static var applicationSupport: DynamicPath { get }
-
Returns
Path.string
Declaration
Swift
public var description: String { get }
-
Returns eg.
Path(string: "/foo")
Declaration
Swift
public var debugDescription: String { get }