Skip to content

Instantly share code, notes, and snippets.

@johnny77221
Forked from saoudrizwan/Storage.swift
Last active June 15, 2018 00:31
Show Gist options
  • Select an option

  • Save johnny77221/28f559c9eafa47a33a07f121231c6278 to your computer and use it in GitHub Desktop.

Select an option

Save johnny77221/28f559c9eafa47a33a07f121231c6278 to your computer and use it in GitHub Desktop.

Revisions

  1. johnny77221 revised this gist Jun 15, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Storage.swift
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@ public class Storage {
    /// - directory: directory 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, from directory: Directory, as type: T.Type) -> T {
    static func retrieve<T: Decodable>(_ fileName: String, from directory: Directory, as type: T.Type) -> T? {
    let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)

    if !FileManager.default.fileExists(atPath: url.path) {
    @@ -72,10 +72,10 @@ public class Storage {
    let model = try decoder.decode(type, from: data)
    return model
    } catch {
    fatalError(error.localizedDescription)
    return nil // decode fail
    }
    } else {
    fatalError("No data at \(url.path)!")
    return nil // fatalError("No data at \(url.path)!")
    }
    }

  2. @gitroyalty-bot gitroyalty-bot revised this gist Jul 22, 2017. No changes.
  3. @gitroyalty-bot gitroyalty-bot revised this gist Jul 22, 2017. 1 changed file with 3 additions and 14 deletions.
    17 changes: 3 additions & 14 deletions Storage.swift
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,8 @@ public class Storage {
    case caches
    }

    static func getURL(for directory: Directory) -> URL {
    /// Returns URL constructed from specified directory
    static fileprivate func getURL(for directory: Directory) -> URL {
    var searchPathDirectory: FileManager.SearchPathDirectory

    switch directory {
    @@ -79,8 +80,6 @@ public class Storage {
    }

    /// Remove all files at specified directory
    ///
    /// - Parameter directory: directory to clear
    static func clear(_ directory: Directory) {
    let url = getURL(for: directory)
    do {
    @@ -94,10 +93,6 @@ public class Storage {
    }

    /// Remove specified file from specified directory
    ///
    /// - Parameters:
    /// - fileName: file to delete
    /// - directory: directory where file is located
    static func remove(_ fileName: String, from directory: Directory) {
    let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
    if FileManager.default.fileExists(atPath: url.path) {
    @@ -109,15 +104,9 @@ public class Storage {
    }
    }

    /// Checks if file exists at specified directory with specified file name
    ///
    /// - Parameters:
    /// - fileName: name of file
    /// - directory: directory where file is located
    /// - Returns: BOOL value indicating whether file exists at location or not
    /// Returns BOOL indicating whether file exists at specified directory with specified file name
    static func fileExists(_ fileName: String, in directory: Directory) -> Bool {
    let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
    return FileManager.default.fileExists(atPath: url.path)
    }

    }
  4. @gitroyalty-bot gitroyalty-bot created this gist Jul 22, 2017.
    123 changes: 123 additions & 0 deletions Storage.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    import Foundation

    public class Storage {

    fileprivate init() { }

    enum Directory {
    // Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud.
    case documents

    // Data that can be downloaded again or regenerated should be stored in the <Application_Home>/Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.
    case caches
    }

    static func getURL(for directory: Directory) -> URL {
    var searchPathDirectory: FileManager.SearchPathDirectory

    switch directory {
    case .documents:
    searchPathDirectory = .documentDirectory
    case .caches:
    searchPathDirectory = .cachesDirectory
    }

    if let url = FileManager.default.urls(for: searchPathDirectory, in: .userDomainMask).first {
    return url
    } else {
    fatalError("Could not create URL for specified directory!")
    }
    }


    /// Store an encodable struct to the specified directory on disk
    ///
    /// - Parameters:
    /// - object: the encodable struct to store
    /// - directory: where to store the struct
    /// - fileName: what to name the file where the struct data will be stored
    static func store<T: Encodable>(_ object: T, to directory: Directory, as fileName: String) {
    let url = getURL(for: directory).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)
    }
    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
    /// - directory: directory 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, from directory: Directory, as type: T.Type) -> T {
    let url = getURL(for: directory).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 all files at specified directory
    ///
    /// - Parameter directory: directory to clear
    static func clear(_ directory: Directory) {
    let url = getURL(for: directory)
    do {
    let contents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])
    for fileUrl in contents {
    try FileManager.default.removeItem(at: fileUrl)
    }
    } catch {
    fatalError(error.localizedDescription)
    }
    }

    /// Remove specified file from specified directory
    ///
    /// - Parameters:
    /// - fileName: file to delete
    /// - directory: directory where file is located
    static func remove(_ fileName: String, from directory: Directory) {
    let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
    if FileManager.default.fileExists(atPath: url.path) {
    do {
    try FileManager.default.removeItem(at: url)
    } catch {
    fatalError(error.localizedDescription)
    }
    }
    }

    /// Checks if file exists at specified directory with specified file name
    ///
    /// - Parameters:
    /// - fileName: name of file
    /// - directory: directory where file is located
    /// - Returns: BOOL value indicating whether file exists at location or not
    static func fileExists(_ fileName: String, in directory: Directory) -> Bool {
    let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
    return FileManager.default.fileExists(atPath: url.path)
    }

    }