Skip to content

Ryu0118/FileManagerProtocol

Repository files navigation

FileManagerProtocol

A Swift protocol that abstracts FileManager operations for improved testability and flexibility.

Requirements

  • Swift 6.0+
  • macOS 13.0+ / iOS 16.0+ / tvOS 16.0+ / watchOS 9.0+ / visionOS 1.0+

Installation

Swift Package Manager

Add the following to your Package.swift:

dependencies: [
    .package(url: "https://github.com/Ryu0118/FileManagerProtocol.git", from: "0.1.0")
]

Usage

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)
}

Temporary Directory

// 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) }

License

MIT License

About

A Swift protocol that abstracts FileManager operations for improved testability and flexibility.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages