A Swift class to help save and retrieve files from iOS devices. To use, import FileSaveHelper.swift to your project.
There are three init methods provided. The default directory is the Document directory and the default sub directory is empty.
If you don't want to specify the directory or sub directory for the file use:
let file = JJJFile(fileName: "myTextFile", fileExtension: .txt)
To specify a sub directory use:
let file = JJJFile(fileName: "myTextFile", fileExtension: .txt, subDirectory: "files")
To specify the sub directory and the directory use:
let file = JJJFile(fileName: "myTextFile", fileExtension: .txt, subDirectory: "files", directory: .documentDirectory)
If you want to save a text file, you have to give it content to save and the save method throws.
let textToSave = "Saved a file in Swift!"
do {
try file.save(textToSave)
}
catch {
print("There was an error saving the file: \(error)")
}
To check if a file exists, you use the fileExists
property
To get the contents of a file use the getContentsOfFile
like so:
do {
let contents = try file.getContents()
}
catch {
print("There was an error getting the contents: \(error)")
}