CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Jul 2016 |
| SPMSupports SPM | ✓ |
Maintained by Cihat Gündüz.
Installation • Usage • License • Documentation
FileKit is a Swift framework that allows for simple and expressive file management.
Development happens in the develop branch.
OS X 10.9+ / iOS 8.0+ / watchOS 2.0 / tvOS 9.0
Xcode 7.1+, Swift 2.1+
Paths are handled with the Path structure.
let home = Path("~")
let drive: Path = "/Volumes/Macintosh HD"
let file: Path = "~/Desktop/file\(1)"A blank file can be written by calling createFile() on an Path.
try Path(".gitignore").createFile()A directory can be created by calling createDirectory() on an Path.
try Path("~/Files").createDirectory()
try Path("~/Books").createDirectory(withIntermediateDirectories: false)Intermediate directories are created by default.
A symbolic link can be created by calling createSymlinkToPath(_:) on an Path.
try Path("path/to/MyApp.app").createSymlinkToPath("~/Applications")
print(Path("~/Applications/MyApp.app").exists) // trueYou can find all paths with the “.txt” extension five folders deep into the Desktop with:
let textFiles = Path.UserDesktop.find(searchDepth: 5) { path in
path.pathExtension == "txt"
}A negative searchDepth will make it run until every path in self is checked against.
You can even map a function to paths found and get the non-nil results:
let documents = Path.UserDocuments.find(searchDepth: 1) { path in
String(path)
}Because Path conforms to SequenceType, it can be iterated through with a for loop.
for download in Path.UserDownloads {
print("Downloaded file: \(download)")
}The current working directory for the process can be changed with Path.Current.
To quickly change the current working directory to a path and back, there’s the changeDirectory(_:) method:
Path.UserDesktop.changeDirectory {
print(Path.Current) // "/Users/nvzqz/Desktop"
}A common ancestor between two paths can be obtained:
print(Path.Root.commonAncestor(.UserHome)) // "/"
print("~/Desktop" <^> "~/Downloads") // "~"
print(.UserLibrary <^> .UserApplicationSupport) // "/Users/nvzqz/Library"+ OperatorAppends two paths and returns the result
// ~/Documents/My Essay.docx
let essay = Path.UserDocuments + "My Essay.docx"It can also be used to concatenate a string and a path, making the string value a Path beforehand.
let numberedFile: Path = "path/to/dir" + String(10) // "path/to/dir/10"+= OperatorAppends the right path to the left path. Also works with a String.
var photos = Path.UserPictures + "My Photos" // ~/Pictures/My Photos
photos += "../My Other Photos" // ~/Pictures/My Photos/../My Other Photos% OperatorReturns the standardized version of the path.
let path: Path = "~/Desktop"
path% == path.standardized // true* OperatorReturns the resolved version of the path.
let path: Path = "~/Documents"
path* == path.resolved // true^ OperatorReturns the path’s parent path.
let path: Path = "~/Movies"
path^ == "~" // true->> OperatorMoves the file at the left path to the right path.
Path counterpart: moveFileToPath(_:)
File counterpart: moveToPath(_:)
->! OperatorForcibly moves the file at the left path to the right path by deleting anything at the left path before moving the file.
+>> OperatorCopies the file at the left path to the right path.
Path counterpart: copyFileToPath(_:)
File counterpart: copyToPath(_:)
+>! OperatorForcibly copies the file at the left path to the right path by deleting anything at the left path before copying the file.
=>> OperatorCreates a symlink of the left path at the right path.
Path counterpart: symlinkFileToPath(_:)
File counterpart: symlinkToPath(_:)
=>! OperatorForcibly creates a symlink of the left path at the right path by deleting anything at the left path before creating the symlink.
Subscripting an Path will return all of its components up to and including the index.
let users = Path("/Users/me/Desktop")[1] // /Usersstandardize() Standardizes the path.
The same as doing:
somePath = somePath.standardizedresolve() Resolves the path’s symlinks.
The same as doing:
somePath = somePath.resolvedA file can be made using File with a DataType for its data type.
let plistFile = File<NSDictionary>(path: Path.UserDesktop + "sample.plist")Files can be compared by size.
|> OperatorWrites the data on the left to the file on the right.
do {
try "My name is Bob." |> TextFile(path: Path.UserDesktop + "name.txt")
} catch {
print("I can't write to a desktop file?!")
}The TextFile class allows for reading and writing strings to a file.
Although it is a subclass of File<String>, TextFile offers some functionality that File<String> doesn’t.
|>> OperatorAppends the string on the left to the TextFile on the right.
let readme = TextFile(path: "README.txt")
try "My Awesome Project" |> readme
try "This is an awesome project." |>> readmeA typealias to File<NSDictionary>.
A typealias to File<NSArray>
A typealias to File<NSData>
The FilePermissions struct allows for seeing the permissions of the current process for a given file.
let swift: Path = "/usr/bin/swift"
print(swift.filePermissions) // FilePermissions[Read, Execute]All types that conform to DataType can be used to satisfy the generic type for File.
A Readable type must implement the static method readFromPath(_:).
All Readable types can be initialized with init(contentsOfPath:).
A Writable type must implement writeToPath(_:atomically:).
Writing done by writeToPath(_:) is done atomically by default.
Types that have a writeToFile(_:atomically:) method that takes in a String for the file path can conform to Writable by simply conforming to WritableToFile.
If a type itself cannot be written to a file but can output a writable type, then it can conform to WritableConvertible and become a Writable that way.
The type for all errors thrown by FileKit operations is FileKitError.
Errors can be converted to String directly for any logging. If only the error message is needed, FileKitError has a message property that states why the error occurred.
// FileKitError(Could not copy file from "path/to/file" to "path/to/destination")
String(FileKitError.CopyFileFail(from: "path/to/file", to: "path/to/destination"))FileKit and its assets are released under the MIT License. Assets can be found in the assets branch.