A Swift protocol that abstracts FileManager operations for improved testability and flexibility.
- Swift 6.0+
- macOS 13.0+ / iOS 16.0+ / tvOS 16.0+ / watchOS 9.0+ / visionOS 1.0+
Add the following to your Package.swift:
dependencies: [
.package(url: "https://github.com/Ryu0118/FileManagerProtocol.git", from: "0.1.0")
]FileManager conforms to FileManagerProtocol out of the box:
import FileManagerProtocol
func saveData(_ data: Data, using fileManager: FileManagerProtocol = FileManager.default) throws {
let url = fileManager.temporaryDirectory.appendingPathComponent("file.txt")
fileManager.createFile(atPath: url.path, contents: data)
}// Automatic cleanup after closure
let result = try await fileManager.runInTemporaryDirectory { tempDir in
let file = tempDir.appendingPathComponent("data.json")
try data.write(to: file)
return try processFile(at: file)
}
// Manual management
let tempDir = try fileManager.makeTemporaryDirectory(prefix: "MyApp")
defer { try? fileManager.removeItem(at: tempDir) }MIT License