Skip to content

Latest commit

 

History

History
85 lines (70 loc) · 1.79 KB

README.md

File metadata and controls

85 lines (70 loc) · 1.79 KB

Persistible

Helper for Persisting Codable classes to file and retrieving them

Create a model object that conforms to Codable and Hashable and give an appropriate fileName after conforming to Persistible

Example
struct Person: Codable, Hashable, Persistible {

    let name: String
    let age: Int
    let school: String

    static var fileName: String {
        return "person_data"
    }
}

Get instances of your object, for instance from the return of a network call helper.

let sam = Person(name: "Samuel", age: 35, school: "Accra Academy")
let amy = Person(name: "Amy", age: 25, school: "Accra Girls")
let maame = Person(name: "Maame", age: 25, school: "Accra High")

To save to file for array, do:

do {
    try [sam, amy].saveToFile()
}
catch {
    print(error)
}

or save for single instance, do:

do {
    try sam.saveToFile()
}
catch {
    print(error)
}

To read your object from file, do one of the below: You can use a do-catch block based on preference.

  1. Single instance
    let person = try? Person.loadFromFile()
  1. Array instance
    let people = try? Person.loadListFromFile()
  1. Loads single or array wrapped inside a PersistenceData enum value based on what it finds. This is the preferred usage.
    do {
        try Person.load { data in

            switch data {

            case .single(let val):
                print("\(val.name), \(val.age), \(val.school)")

            case .array(let values):
                print(values.compactMap { "\($0.name), \($0.age), \($0.school)" })
            }
        }
    }
    catch {
        print(error.localizedDescription)
    }

Installation

The easiest way to install it is by copying the following to your project:

  • Persistible.swift