Installation • Usage • License
FileKit is a Swift framework that allows for simple and expressive file management.
FileKit currently only supports Swift 2.0 and Xcode 7.
- Xcode
- Version: 7.0 Beta 6 (haven't tested previous betas)
- 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
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.0.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"
Property | Type | Value |
---|---|---|
Current |
FKPath |
The program's current working directory |
Separator |
String |
"/" |
rawValue |
String |
The path as a string |
components |
[FKPath] |
The path's components |
parent |
FKPath |
The path's parent path |
children |
[FKPath] |
The path's child paths |
standardized |
FKPath |
The path by removing extraneous components |
resolved |
FKPath |
The path by resolving all symlinks |
absolute |
FKPath |
The path as absolute (begins with "/" ) |
isAbsolute |
Bool |
True if the path begins with "/" |
isRelative |
Bool |
True if the path does not begin with "/" |
isDirectory |
Bool |
True if the path refers to a directory |
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")
}
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
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.