FilePath
Represents a location in the file system.
Declaration
struct FilePathOverview
This structure recognizes directory separators (e.g. /), roots, and requires that the content terminates in a NUL (0x0). Beyond that, it does not give any meaning to the bytes that it contains. The file system defines how the content is interpreted; for example, by its choice of string encoding.
On construction, FilePath will normalize separators by removing redundant intermediary separators and stripping any trailing separators. On Windows, FilePath will also normalize forward slashes / into backslashes \, as preferred by the platform.
The code below creates a file path from a string literal, and then uses it to open and append to a log file:
let message: String = "This is a log message."
let path: FilePath = "/tmp/log"
let fd = try FileDescriptor.open(path, .writeOnly, options: .append)
try fd.closeAfter { try fd.writeAll(message.utf8) }File paths conform to the Equatable and Hashable protocols by performing the protocols’ operations on their raw byte contents. This conformance allows file paths to be used, for example, as keys in a dictionary. However, the rules for path equivalence are file-system–specific and have additional considerations like case insensitivity, Unicode normalization, and symbolic links.
Topics
Creating a File Path
Working with File Paths
Interacting with C APIs
Structures
Initializers
init(_:)init(_:)init(cString:)init(cString:)init(cString:)init(cString:)init(platformString:)init(platformString:)init(platformString:)init(platformString:)init(root:_:)init(root:_:)init(root:components:)
Instance Properties
Instance Methods
append(_:)append(_:)append(_:)appending(_:)appending(_:)appending(_:)ends(with:)lexicallyNormalize()lexicallyNormalized()lexicallyResolving(_:)push(_:)pushing(_:)removeAll(keepingCapacity:)removeLastComponent()removePrefix(_:)removingLastComponent()removingRoot()reserveCapacity(_:)starts(with:)withPlatformString(_:)