withFileHandle(forReadingAndWritingAt:options:execute:)

Opens the file at the given path and provides scoped read-write access to it.

FileSystemProtocol.swift:385
iOS
13.0+
macOS
10.15+
tvOS
13.0+
watchOS
6.0+
func withFileHandle<Result>(forReadingAndWritingAt path: FilePath, options: OpenOptions.Write = .newFile(replaceExisting: false), execute: (_ readWrite: ReadWriteFileHandle) async throws -> Result) async throws -> Result

Parameters

path

The path of the file to open for reading and writing.

options

How the file should be opened.

execute

A closure which provides access to the open file. The file is closed automatically after the closure exits.

Returns

The result of the execute closure.

The file remains open during lifetime of the execute block and will be closed automatically before the function returns. Files may also be opened in read-only or write-only mode by with withFileHandle(forReadingAt:options:execute:) and withFileHandle(forWritingAt:options:execute:).