Storage

public class Storage

Storage can be used to save or cache objects that conform to Codable protocl.

See also

Codable
  • Storage errors

    See more

    Declaration

    Swift

    public enum StorageError : Error
  • File type, currently supporting .json

    See more

    Declaration

    Swift

    public enum FileType : String
  • Directory to save data

    See more

    Declaration

    Swift

    public enum Directory
  • Store an encodable struct to the specified directory on disk

    Declaration

    Swift

    public static func store<T>(_ object: T, to directory: Directory, as fileType: FileType, withName fileName: String) throws where T : Encodable

    Parameters

    object

    the encodable struct to store

    directory

    where to store the struct. It is recommended by Apple to use the Library directory, which will save the app data files into the Library/Application Support Directory. The contents of this directory are backed up by iTunes and iCloud.

    fileName

    what to name the file where the struct data will be stored

  • Retrieve and convert a struct from a file on disk

    Example: A simple Resort object
    let resorts = retrieve("resorts", of: .json, from: .documents, as: [Resort].self)
    

    Declaration

    Swift

    public static func retrieve<T>(_ fileName: String, of fileType: FileType, from directory: Directory, as type: T.Type) throws -> T? where T : Decodable

    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)

    Return Value

    decoded struct model(s) of data

  • Remove all files at specified directory

    Declaration

    Swift

    public static func clear(_ directory: Directory) throws

    Parameters

    directory

    The directory to clean

  • Remove specified file from specified directory

    Declaration

    Swift

    public static func remove(_ fileName: String, of fileType: FileType, from directory: Directory) throws

    Parameters

    fileName

    The file name

    fileType

    The file type

    directory

    The directory to remove the file

  • Returns BOOL indicating whether file exists at specified directory with specified file name

    Declaration

    Swift

    public static func fileExists(_ fileName: String, of fileType: FileType, in directory: Directory) throws -> Bool

    Parameters

    fileName

    The file name

    fileType

    The file type

    directory

    The directory to check if the file exists