Installation • Usage • License • Documentation
FileKit is a Swift framework that allows for simple and expressive file management.
Development happens in the
develop
branch.
- Xcode
- Version: 7.0
- Language: Swift 2.0
- OS X
- Compatible With: OS X 10.11
- Deployment Target: OS X 10.9
- iOS
- Compatible With: iOS 9.0
- Deployment Target: iOS 8.0
- watchOS
- Compatible With: watchOS 2.0
- Deployment Target: watchOS 2.0
CocoaPods is a centralized dependency manager for Objective-C and Swift. Go here to learn more.
-
Add the project to your Podfile.
use_frameworks! pod 'FileKit', '~> 1.5.0'
-
Run
pod install
and open the.xcworkspace
file to launch Xcode. -
Import the FileKit framework.
import FileKit
Carthage is a decentralized dependency manager for Objective-C and Swift.
-
Add the project to your Cartfile.
github "nvzqz/FileKit"
-
Run
carthage update
and follow the additional steps in order to add FileKit to your project. -
Import the FileKit framework.
import FileKit
Paths are handled with the FKPath
structure.
Paths can be created with an initializer or with a string literal.
let home = FKPath("~")
let drive: FKPath = "/Volumes/Macintosh HD"
A blank file can be written by calling createFile()
on an FKPath
.
do {
try FKPath(".gitignore").createFile()
} catch {
print("Could not create .gitignore")
}
A directory can be created by calling createDirectory()
on an FKPath
.
do {
try FKPath("~/Files").createDirectory()
} catch {
print("Could not create Files")
}
A symbolic link can be created by calling createSymlinkToPath(_:)
on an FKPath
.
do {
let filePath = FKPath.UserDesktop + "text.txt"
try filePath.createFile()
let linkPath = FKPath.UserDesktop + "link.txt"
try filePath.createSymlinkToPath(linkPath)
print(linkPath.exists) // true
let text = "If she weighs the same as a duck, she's made of wood."
try text |> FKTextFile(path: filePath)
let contents = try FKTextFile(path: linkPath).read()
print(contents == text) // true
} catch {
print("Could not create symlink")
}
You can find all paths with the ".txt" extension five folders deep into the Desktop with:
let textFiles = FKPath.UserDesktop.findPaths(searchDepth: 5) { path in
path.pathExtension == "txt"
}
Setting searchDepth
to a negative value will make it run until every path in
self
is checked against. If the checked path passes the condition, it'll be
added to the returned paths and the next path will be checked. If it doesn't and
it's a directory, its children paths will be checked.
Appends two paths and returns the result
// ~/Documents/My Essay.docx
let essay = FKPath.UserDocuments + "My Essay.docx"
Appends the right path to the left path
var photos = FKPath.UserPictures + "My Photos" // ~/Pictures/My Photos
photos += "../My Other Photos" // ~/Pictures/My Photos/../My Other Photos
Returns the standardized version of the path.
let path: FKPath = "~/Desktop"
path• == path.standardized // true
Returns the path's parent path.
let path: FKPath = "~/Movies"
path^ == "~" // true
Moves the file at the left path to the right path.
FKPath
counterpart: moveFileToPath(_:)
FKFileType
counterpart: moveToPath(_:)
Forcibly moves the file at the left path to the right path by deleting anything at the left path before moving the file.
Copies the file at the left path to the right path.
FKPath
counterpart: copyFileToPath(_:)
FKFileType
counterpart: copyToPath(_:)
Forcibly copies the file at the left path to the right path by deleting anything at the left path before copying the file.
Creates a symlink of the left path at the right path.
FKPath
counterpart: symlinkFileToPath(_:)
FKFileType
counterpart: symlinkToPath(_:)
Forcibly creates a symlink of the left path at the right path by deleting anything at the left path before creating the symlink.
Subscripting an FKPath
will return all of its components up to and including
the index.
let users = FKPath("/Users/me/Desktop")[1] // /Users
Standardizes the path.
The same as doing:
somePath = somePath.standardized
Resolves the path's symlinks.
The same as doing:
somePath = somePath.resolved
Files are represented with the FKFileType
protocol.
Writes the data on the left to the file on the right.
do {
try "My name is Bob." |> FKTextFile(path: FKPath.UserDesktop + "name.txt")
} catch {
print("I can't write to a desktop file?!")
}
A file can be made using FKFile
with an FKDataType
for its data type.
The generic constraint defines the file's data type.
let textFile = FKFile<String>(path: FKPath.UserDesktop + "sample.txt")
The FKTextFile
class allows for reading and writing strings to a file.
Although it is a subclass of FKFile<String>
, FKTextFile
offers some functionality
that FKFile<String>
doesn't.
Appends the string on the left to the FKTextFile
on the right.
let readme = FKTextFile(path: "README.txt")
do {
try "My Awesome Project" |> readme
try "This is an awesome project." |>> readme
} catch {
print("Could not write to \(readme.path)")
}
The FKDictionaryFile
class allows for reading and writing dictionaries to a file.
It is not a subclass of FKFile
but still conforms to FKFileType
.
do {
let df = FKDictionaryFile(path: FKPath.UserDesktop + "Sample.plist")
let someDictionary: NSDictionary = ["FileKit" : true,
"Hello" : "World"]
try someDictionary |> df
} catch {
print("Something went wrong :( ...")
}
FileKit and its assets are released under the MIT License. Assets
can be found in the assets
branch.