Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sketchytech/SwiftFiles
Save, Load and Delete from a Swift Playground
https://github.com/sketchytech/SwiftFiles
Last synced: 3 months ago
JSON representation
Save, Load and Delete from a Swift Playground
- Host: GitHub
- URL: https://github.com/sketchytech/SwiftFiles
- Owner: sketchytech
- License: mit
- Created: 2015-04-29T13:14:56.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-05-08T10:41:46.000Z (over 8 years ago)
- Last Synced: 2024-04-22T12:31:57.248Z (7 months ago)
- Language: Swift
- Size: 112 KB
- Stars: 40
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: License
Awesome Lists containing this project
- Awesome-Swift-Playgrounds - SwiftFiles - Save, Load and Delete files easily from within a Swift playground. ⏳ (Miscellaneous / SpriteKit)
README
# SwiftFiles
Save, Load and Delete files from within a Swift playground
# Usage
List file NSURLs in a given location
```
do {
if let files = try FileList.allFilesAndFolders(inDirectory: NSSearchPathDirectory.CachesDirectory, subdirectory: "Data") {
for file in files {
print("Filename:\(file.lastPathComponent!) NSURL:\(file)")
}
}
else {
print("failed to find given location")
}
}
catch {
print("error occurred")
}
```
Save a string to a file
```
FileSave.saveStringToTemporaryDirectory("hello World", path: "myString.txt", subdirectory: "")
```
Save, load and delete
```
FileSave.saveString("hello world", directory: NSSearchPathDirectory.DocumentDirectory, path: "myString.txt", subdirectory: "MyFiles")
FileLoad.loadString("myString.txt", directory: NSSearchPathDirectory.DocumentDirectory, subdirectory: "MyFiles", encoding: NSUTF8StringEncoding)
FileDelete.deleteFile("myString.txt", directory: NSSearchPathDirectory.DocumentDirectory, subdirectory: "MyFiles")
FileLoad.loadString("myString.txt", directory: NSSearchPathDirectory.DocumentDirectory, subdirectory: "MyFiles", encoding: NSUTF8StringEncoding) // returns nil file is deleted
```
Save and load data
```
let data = "Hello Swift".dataUsingEncoding(NSUTF8StringEncoding)!
FileSave.saveData(data, directory: NSSearchPathDirectory.CachesDirectory, path: "myFile.txt", subdirectory: "Data")
FileLoad.loadData("myFile.txt", directory: NSSearchPathDirectory.CachesDirectory, subdirectory: "Data") == data // true
```
Find file locations on your system
```
// find out the location of your files to use in Finder -> Go -> Go to Directory dialogue box
FileDirectory.applicationDirectory(NSSearchPathDirectory.DocumentDirectory)
FileDirectory.applicationTemporaryDirectory()
```
Note that the majority of the methods are throwing methods and so require do/try/catch. See the playground for examples.