This page provides documentation for the Filesystem library, which is a convenient way to work with files and directories in your iOS or macOS app. Whether you need to read, write, or manipulate file system entities, this library has got you covered.
Installation
To install the Filesystem library in your project, you have a few options:
- Using CocoaPods: Simply add
filesystem
to yourPodfile
and runpod install
. - Using Carthage: Add
github "johndoe/filesystem"
to yourCartfile
. Then runcarthage update
and link the generated framework with your app. - Manual installation: Download the library from the official GitHub repository (https://github.com/johndoe/filesystem) and manually add the files to your Xcode project.
Usage
Importing the Library
To start using the Filesystem library in your code, import it using the following statement:
import FileSystem
Working with Files
The Filesystem library provides several convenient methods to work with files:
let fileURL = try FileHelper.getURL(for: .documents, fileName: "example.txt")
: Retrieves the URL for a file in the Documents directory.try FileHelper.createFile(at: fileURL, contents: data)
: Creates a new file with the given data at the specified URL.let fileData = try FileHelper.readFile(at: fileURL)
: Reads the contents of a file at the specified URL.try FileHelper.moveFile(from: sourceURL, to: destinationURL)
: Moves a file from one location to another.try FileHelper.deleteFile(at: fileURL)
: Deletes a file at the specified URL.
Working with Directories
In addition to files, the Filesystem library also provides methods to manage directories:
let directoryURL = try DirectoryHelper.getURL(for: .documents)
: Retrieves the URL for a directory in the Documents directory.try DirectoryHelper.createDirectory(at: directoryURL)
: Creates a new directory at the specified URL.let directoryContents = try DirectoryHelper.getDirectoryContents(at: directoryURL)
: Returns an array of URLs representing the contents of a directory.try DirectoryHelper.moveDirectory(from: sourceURL, to: destinationURL)
: Moves a directory from one location to another.try DirectoryHelper.deleteDirectory(at: directoryURL)
: Deletes a directory at the specified URL, including all of its contents.
Examples
Here are a few usage examples of the Filesystem library:
Example 1
// Create a new text file in the Documents directory
let fileURL = try FileHelper.getURL(for: .documents, fileName: "example.txt")
let fileContents = "Hello, World!".data(using: .utf8)
try FileHelper.createFile(at: fileURL, contents: fileContents)
Example 2
// Read the contents of an existing file
let fileURL = try FileHelper.getURL(for: .documents, fileName: "example.txt")
let fileData = try FileHelper.readFile(at: fileURL)
if let contents = String(data: fileData, encoding: .utf8) {
print(contents)
}
Example 3
// Create a new directory and move a file into it
let directoryURL = try DirectoryHelper.getURL(for: .documents, directoryName: "example")
try DirectoryHelper.createDirectory(at: directoryURL)
let sourceFileURL = try FileHelper.getURL(for: .documents, fileName: "example.txt")
let destinationFileURL = directoryURL.appendingPathComponent("example.txt")
try FileHelper.moveFile(from: sourceFileURL, to: destinationFileURL)
Additional Resources
For more detailed information on how to use the Filesystem library, refer to the official documentation and resources:
- GitHub Repository: https://github.com/johndoe/filesystem
- API Reference: https://johndoe.github.io/filesystem/docs/api/