Skip to content

Instantly share code, notes, and snippets.

@stucarney
Forked from saoudrizwan/Storage.swift
Last active July 13, 2018 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stucarney/00a8b56732c89c55313f3ed0387af843 to your computer and use it in GitHub Desktop.
Save stucarney/00a8b56732c89c55313f3ed0387af843 to your computer and use it in GitHub Desktop.
[LINUX SUPPORTED] Helper class to easily store and retrieve Codable structs from/to disk. https://medium.com/@sdrzn/swift-4-codable-lets-make-things-even-easier-c793b6cf29e1
import Foundation
public class Storage {
fileprivate init() { }
/// Get the current working directory
/// - Returns: URL of current working directory
static func getCurrentDirectoryURL() -> URL {
return URL(fileURLWithPath: FileManager.default.currentDirectoryPath, isDirectory: true)
}
/// Store an encodable struct to the current directory on disk
///
/// - Parameters:
/// - object: the encodable struct to store
/// - fileName: what to name the file where the struct data will be stored
static func store<T: Encodable>(_ object: T, as fileName: String) {
let url = getCurrentDirectoryURL().appendingPathComponent(fileName, isDirectory: false)
let encoder = JSONEncoder()
do {
let data = try encoder.encode(object)
if FileManager.default.fileExists(atPath: url.path) {
try FileManager.default.removeItem(at: url)
}
let _ = FileManager.default.createFile(atPath: url.path, contents: data, attributes: nil)
} catch {
fatalError(error.localizedDescription)
}
}
/// Retrieve and convert a struct from a file on disk
///
/// - Parameters:
/// - fileName: name of the file where struct data is stored
/// - type: struct type (i.e. Message.self)
/// - Returns: decoded struct model(s) of data
static func retrieve<T: Decodable>(_ fileName: String, as type: T.Type) -> T {
let url = getCurrentDirectoryURL().appendingPathComponent(fileName, isDirectory: false)
if !FileManager.default.fileExists(atPath: url.path) {
fatalError("File at path \(url.path) does not exist!")
}
if let data = FileManager.default.contents(atPath: url.path) {
let decoder = JSONDecoder()
do {
let model = try decoder.decode(type, from: data)
return model
} catch {
fatalError(error.localizedDescription)
}
} else {
fatalError("No data at \(url.path)!")
}
}
/// Remove specified file from current directory
static func remove(_ fileName: String) {
let url = getCurrentDirectoryURL().appendingPathComponent(fileName, isDirectory: false)
if FileManager.default.fileExists(atPath: url.path) {
do {
try FileManager.default.removeItem(at: url)
} catch {
fatalError(error.localizedDescription)
}
}
}
/// Returns BOOL indicating whether file exists in current directory with specified file name
static func fileExists(_ fileName: String) -> Bool {
let url = getCurrentDirectoryURL().appendingPathComponent(fileName, isDirectory: false)
return FileManager.default.fileExists(atPath: url.path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment