Skip to content

Latest commit

 

History

History
279 lines (199 loc) · 6.63 KB

README.md

File metadata and controls

279 lines (199 loc) · 6.63 KB

Platform Language CocoaPods Carthage License

InstallationUsageLicense

FileKit is a Swift framework that allows for simple and expressive file management.

Installation

Compatibility

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

Install Using CocoaPods

CocoaPods is a centralized dependency manager for Objective-C and Swift. Go here to learn more.

  1. Add the project to your Podfile.

    use_frameworks!
    
    pod 'FileKit', '~> 1.0.0'
    
  2. Run pod install and open the .xcworkspace file to launch Xcode.

  3. Import the FileKit framework.

    import FileKit

Install Using Carthage

Carthage is a decentralized dependency manager for Objective-C and Swift.

  1. Add the project to your Cartfile.

    github "nvzqz/FileKit"
    
  2. Run carthage update and follow the additional steps in order to add FileKit to your project.

  3. Import the FileKit framework.

    import FileKit

Usage

Paths

Paths are handled with the FKPath structure.

Initialization

Paths can be created with an initializer or with a string literal.

let home = FKPath("~")
let drive: FKPath = "/Volumes/Macintosh HD"

Properties

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

Operations

New Files

A blank file can be written by calling createFile() on an FKPath.

do {
    try FKPath(".gitignore").createFile()
} catch {
    print("Could not create .gitignore")
}
New Directories

A directory can be created by calling createDirectory() on an FKPath.

do {
    try FKPath("~/Files").createDirectory()
} catch {
    print("Could not create Files")
}
+ Operator

Appends two paths and returns the result

// ~/Documents/My Essay.docx
let essay  = FKPath.UserDocuments + "My Essay.docx"
+= Operator

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
Operator (alt+8)

Returns the standardized version of the path.

let path: FKPath = "~/Desktop"
path == path.standardized  // true
^ Operator

Returns the path's parent path.

let path: FKPath = "~/Movies"
path^ == "~"  // true
Subscripting

Subscripting an FKPath will return all of its components up to and including the index.

let users = FKPath("/Users/me/Desktop")[1]  // /Users
standardize()

Standardizes the path.

The same as doing:

somePath = somePath.standardized
resolve()

Resolves the path's symlinks.

The same as doing:

somePath = somePath.resolved

Files

FKFileType

Files are represented with the FKFileType protocol.

|> Operator

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?!")
}

FKFile

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

FKTextFile

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.

|>> Operator

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

FKDictionaryFile

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 :( ...")
}

License

FileKit and its assets are released under the MIT License. Assets can be found in the assets branch.