Giter Club home page Giter Club logo

swiftfilepath's Introduction

SwiftFilePath

CI Status Carthage Compatibility Version License Platform

Simple and powerful wrapper for NSFileManager.

Usage

Create instance

You can create Path object from String.

let fooDir = Path("/path/to/fooDir")

// You can re obtain String by calling toString.
fooDir.toString() // "/path/to/fooDir"

Get accessible Path from App by factory methods.

Path.homeDir.toString()
// "/var/mobile/Containers/Data/Application/<UUID>"

Path.documentsDir.toString()
// "/var/mobile/Containers/Data/Application/<UUID>/Documents"

Path.cacheDir.toString()
// "var/mobile/Containers/Data/Application/<UUID>/Library/Caches"

Path.temporaryDir.toString()
// "var/mobile/Containers/Data/Application/<UUID>/Library/tmp"

Access to other directories and files

//  Get Path that indicate foo.txt file in Documents dir
let textFilePath = Path.documentsDir["foo.txt"]
textFilePath.toString() //  "~/Documents/foo.txt"

//  You can access subdir.
let jsonFilePath = Path.documentsDir["subdir"]["bar.json"]
jsonFilePath.toString() //  "~/Documents/subdir/bar.json"

// Access to parent Path.
jsonFilePath.parent.toString() // "~/Documents/subdir"
jsonFilePath.parent.parent.toString() // "~/Documents"
jsonFilePath.parent.parent.parent.toString() // "~/"
let contents = Path.homeDir.contents!
//  Get dir contents as Path object.
// [
//    Path<~/.com.apple.mobile_container_manager.metadata.plist>, 
//    Path<~/Documents>,
//    Path<~/Library>, 
//    Path<~/tmp>,
// ]

// Or you can use dir as iterator
for content:Path in Path.homeDir { 
    println(content) 
 }

Access to file infomation

Check if path is dir or not.

Path.homeDir.isDir // true
Path.homeDir["hoge.txt"].isDir //false

Check if path is exists or not.

// homeDir is exists
Path.homeDir.exists // true

// Is there foo.txt in homeDir ?
Path.homeDir["foo.txt"].exists

// Is there foo.txt in myDir ?
Path.homedir["myDir"]["bar.txt"].exists

You can get basename of file.

Path.homedir["myDir"]["bar.txt"].basename // bar.txt

You can get file extension.

//  Get all *json files in Documents dir.
let allFiles  = Path.documentsDir.contents!
let jsonFiles = allFiles.filter({$0.ext == "json" })

You can get more attributes of file.

let jsonFile = Path.documentsDir["foo.json"]
jsonFile.attributes!.fileCreationDate()! // 2015-01-11 11:30:11 +0000
jsonFile.attributes!.fileModificationDate()! // 2015-01-11 11:30:11 +0000
jsonFile.attributes!.fileSize() // 2341

File operation

Create ( or delete ) dir and files.

// Create "foo" dir in Documents.
let fooDir = Path.documentsDir["foo"]
fooDir.mkdir()

//  Create empty file "hoge.txt" in "foo" dir.
let hogeFile = fooDir["hoge.txt"]
hogeFile.touch()

// Delete foo dir
fooDir.remove()

Copy ( or move ) file.

let fooFile = Path.documentsDir["foo.txt"]
let destination = Path.tmpDir["foo.txt"]
fooFile.copyTo( destination )

Write ( or read ) string data.

// Write string.
let textFile = Path.documentsDir["hello.txt"]
textFile.writeString("HelloSwift")

// Read string.
let text = textFile.readString()! // HelloSwift

Write ( or read ) binary data.

//  Write binary data.
let binFile = Path.documentsDir["foo.bin"]
binFile.writeData( NSData()  )

// Read  binary data.
let data = binFile.readData()!

Error handling

touch/remove/copyTo/writeTo/mkdir returns Result as Enum.

If operation is success, Result has value property. If operation is failure,Result has error property.

let result = Path.documentsDir["subdir"].mkdir()
if( result.isSuccess ){ 
    println( result.value! ) 
}
if( result.isFailure ){ 
    println( result.error! ) 
}

Or you can write by closure style. ( You use this style, you don't need to unwrap optional value )

Path.documentsDir["subdir"].mkdir()
    .onSuccess({ (value:Path) in 
        println( value )
    })
    .onFailure({ (error:NSError) in 
        println( error )
    })

Installation

CocoaPods

To install it, simply add the following line to your Podfile:

pod  'SwiftFilePath',

( Currently, to install the framework via CocoaPods you need to use pre-release version.)

Carthage

To install it, simply add the following line to your Cartfile:

github "nori0620/SwiftFilePath"

LICENSE

SwiftPathClass is released under the MIT license. See LICENSE for details.

swiftfilepath's People

Contributors

jacobvanorder avatar mono0926 avatar nori0620 avatar woildan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.