Skip to content

Repository files navigation

SwiftFilePath

CI StatusCarthage CompatibilityVersionLicensePlatform

Simple and powerful wrapper for NSFileManager.

Usage

Create instance

You can create Path object from String.

letfooDir=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
lettextFilePath=Path.documentsDir["foo.txt"]
textFilePath.toString() // "~/Documents/foo.txt"
// You can access subdir.
letjsonFilePath=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() // "~/"
letcontents=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
forcontent:PathinPath.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.
letallFiles=Path.documentsDir.contents!
letjsonFiles= allFiles.filter({$0.ext =="json"})

You can get more attributes of file.

letjsonFile=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.
letfooDir=Path.documentsDir["foo"]
fooDir.mkdir()
// Create empty file "hoge.txt" in "foo" dir.
lethogeFile=fooDir["hoge.txt"]
hogeFile.touch()
// Delete foo dir
fooDir.remove()

Copy ( or move ) file.

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

Write ( or read ) string data.

// Write string.
lettextFile=Path.documentsDir["hello.txt"]
textFile.writeString("HelloSwift")
// Read string.
lettext= textFile.readString()! // HelloSwift

Write ( or read ) binary data.

// Write binary data.
letbinFile=Path.documentsDir["foo.bin"]
binFile.writeData(NSData())
// Read binary data.
letdata= 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.

letresult=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)inprintln( value )}).onFailure({(error:NSError)inprintln( 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.

About

Simple and powerful wrapper for NSFileManager.

Resources

Stars

289 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages