|
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/nori0620/SwiftFilePath) |
4 | 4 |
|
| 5 | +Simple and powerful wrapper for NSFileManager. |
| 6 | + |
| 7 | + |
| 8 | +# Usage |
| 9 | + |
| 10 | +## Create instance |
| 11 | + |
| 12 | +You can create `Path` object from String. |
| 13 | + |
| 14 | +```swift |
| 15 | +let fooDir = Path("/path/to/fooDir") |
| 16 | + |
| 17 | +// You can re obtain String by calling toString. |
| 18 | +fooDir.toString() // "/path/to/fooDir" |
| 19 | +```` |
| 20 | + |
| 21 | +Get accessible `Path` from App by factory methods. |
| 22 | + |
| 23 | +```swift |
| 24 | +Path.homeDir.toString() |
| 25 | +// "/var/mobile/Containers/Data/Application/<UUID>" |
| 26 | + |
| 27 | +Path.documentsDir.toString() |
| 28 | +// "/var/mobile/Containers/Data/Application/<UUID>/Documents" |
| 29 | + |
| 30 | +Path.cacheDir.toString() |
| 31 | +// "var/mobile/Containers/Data/Application/<UUID>/Library/Caches" |
| 32 | + |
| 33 | +Path.temporaryDir.toString() |
| 34 | +// "var/mobile/Containers/Data/Application/<UUID>/Library/tmp" |
| 35 | +```` |
| 36 | + |
| 37 | +## Directories and files |
| 38 | + |
| 39 | +```swift |
| 40 | +// Get Path that indicate foo.txt file in Documents dir |
| 41 | +let textFilePath = Path.documentsDir["foo.txt"] |
| 42 | +textFilePath.toString() // "~/Documents/foo.txt" |
| 43 | + |
| 44 | +// You can access subdir. |
| 45 | +let jsonFilePath = Path.documentsDir["subdir"]["bar.json"] |
| 46 | +jsonFilePath.toString() // "~/Documents/subdir/bar.json" |
| 47 | + |
| 48 | +// Access to parent Path. |
| 49 | +jsonFilePath.parent.toString() // "~/Documents/subdir" |
| 50 | +jsonFilePath.parent.parent.toString() // "~/Documents" |
| 51 | +jsonFilePath.parent.parent.parent.toString() // "~/" |
| 52 | +```` |
| 53 | + |
| 54 | +```swift |
| 55 | +let contents = Path.homeDir.contents! |
| 56 | +// Get dir contents as Path object. |
| 57 | +// [ |
| 58 | +// Path<~/.com.apple.mobile_container_manager.metadata.plist>, |
| 59 | +// Path<~/Documents>, |
| 60 | +// Path<~/Library>, |
| 61 | +// Path<~/tmp>, |
| 62 | +// ] |
| 63 | + |
| 64 | +// Or you can use dir as iterator |
| 65 | +for content:Path in Path.homeDir { |
| 66 | + println(content) |
| 67 | + } |
| 68 | +```` |
| 69 | + |
| 70 | +## Access to file infomation |
| 71 | + |
| 72 | +Check if path is dir or not. |
| 73 | + |
| 74 | +```swift |
| 75 | +Path.homeDir.isDir // true |
| 76 | +Path.homeDir["hoge.txt"].isDir //false |
| 77 | +```` |
| 78 | +Check if path is exists or not. |
| 79 | + |
| 80 | +```swift |
| 81 | +// homeDir is exists |
| 82 | +Path.homeDir.exists // true |
| 83 | + |
| 84 | +// Is there foo.txt in homeDir ? |
| 85 | +Path.homeDir["foo.txt"].exists |
| 86 | + |
| 87 | +// Is there foo.txt in myDir ? |
| 88 | +Path.homedir["myDir"]["bar.txt"].exists |
| 89 | +```` |
| 90 | + |
| 91 | +You can get basename of file. |
| 92 | + |
| 93 | +```swift |
| 94 | +Path.homedir["myDir"]["bar.txt"].basename // bar.txt |
| 95 | +```` |
| 96 | + |
| 97 | +You can get file extension. |
| 98 | + |
| 99 | +```swift |
| 100 | +// Get all *json files in Documents dir. |
| 101 | +let allFiles = Path.documentsDir.contents! |
| 102 | +let jsonFiles = allFiles.filter({$0.ext == "json" }) |
| 103 | +```` |
| 104 | + |
| 105 | +You can get more attributes of file. |
| 106 | + |
| 107 | +```swift |
| 108 | +let jsonFile = Path.documentsDir["foo.json"] |
| 109 | +jsonFile.attributes!.fileCreationDate()! // 2015-01-11 11:30:11 +0000 |
| 110 | +jsonFile.attributes!.fileModificationDate()! // 2015-01-11 11:30:11 +0000 |
| 111 | +jsonFile.attributes!.fileSize() // 2341 |
| 112 | +```` |
| 113 | + |
| 114 | +## File opeartion |
| 115 | + |
| 116 | +Create ( or delete ) dir and files. |
| 117 | + |
| 118 | +```swift |
| 119 | +// Create "foo" dir in Documents. |
| 120 | +let fooDir = Path.documentsDir["foo"] |
| 121 | +fooDir.mkdir() |
| 122 | + |
| 123 | +// Create empty file "hoge.txt" in "foo" dir. |
| 124 | +let hogeFile = fooDir["hoge.txt"] |
| 125 | +hogeFile.touch() |
| 126 | + |
| 127 | +// Delete foo dir |
| 128 | +fooDir.remove() |
| 129 | +```` |
| 130 | + |
| 131 | +Copy ( or move ) file. |
| 132 | + |
| 133 | +```swift |
| 134 | +let fooFile = Path.documentsDir["foo.txt"] |
| 135 | +let destination = Path.tmpDir["foo.txt"] |
| 136 | +fooFile.copyTo( destination ) |
| 137 | + |
| 138 | +```` |
| 139 | + |
| 140 | +Write ( or read ) string data. |
| 141 | + |
| 142 | +```swift |
| 143 | +// Write string. |
| 144 | +let textFile = Path.documentsDir["hello.txt"] |
| 145 | +textFile.writeString("HelloSwift") |
| 146 | + |
| 147 | +// Read string. |
| 148 | +let text = textFile.readString()! // HelloSwift |
| 149 | +```` |
| 150 | + |
| 151 | +Write ( or read ) binary data. |
| 152 | + |
| 153 | +```swift |
| 154 | +// Wirte binary data. |
| 155 | +let binFile = Path.documentsDir["foo.bin"] |
| 156 | +binFile.writeData( NSData() ) |
| 157 | + |
| 158 | +// Read binary data. |
| 159 | +let data = binFile.readData()! |
| 160 | +```` |
| 161 | + |
| 162 | +## Error handling |
| 163 | + |
| 164 | +`touch`/`remove`/`copyTo`/`writeTo`/`mkdir` returns `Result` Object. |
| 165 | + |
| 166 | +If operation is success, `Result` has `value` property. |
| 167 | +If operation is failure,`Result` has `error` property. |
| 168 | + |
| 169 | + |
| 170 | +```swift |
| 171 | +let result = Path.documentsDir["subdir"].mkdir() |
| 172 | +if( result.isSuccess ){ |
| 173 | + println( result.value! ) |
| 174 | +} |
| 175 | +if( result.isFailure ){ |
| 176 | + println( result.error! ) |
| 177 | +} |
| 178 | +```` |
| 179 | + |
| 180 | +Or you can write by closure style. ( You use this style, you don't need to unwrap optional value ) |
| 181 | + |
| 182 | +```swift |
| 183 | +Path.documentsDir["subdir"].mkdir() |
| 184 | + .onSuccess({ (value:Path) in |
| 185 | + println( value ) |
| 186 | + }) |
| 187 | + .onFailure({ (error:String) in |
| 188 | + println( error ) |
| 189 | + }) |
| 190 | +```` |
| 191 | + |
| 192 | + |
| 193 | +# Installation |
| 194 | + |
| 195 | +## CocoaPods |
| 196 | + |
| 197 | +To install it, simply add the following line to your Podfile: |
| 198 | + |
| 199 | +```ruby |
| 200 | +pod 'SwiftPathClass', |
| 201 | +``` |
| 202 | + |
| 203 | +( Currently, to install the framework via CocoaPods you need to use pre-release version.) |
| 204 | + |
| 205 | +## Carthage |
| 206 | + |
| 207 | +To install it, simply add the following line to your Cartfile: |
| 208 | + |
| 209 | +```ruby |
| 210 | +github "nori0620/SwiftFilePath" |
| 211 | +``` |
| 212 | + |
| 213 | + |
5 | 214 | # LICENSE |
6 | 215 |
|
7 | 216 | SwiftPathClass is released under the MIT license. See LICENSE for details. |
0 commit comments