Struct persistance framework for Swift. Simple as that:
let myStruct = MyStruct(...)
Depot.persist(myStruct, "cached_data")
if let retrievedStruct: MyStruct = Depot.retrieved("cached_data") {
print("MyStruct is retrieved",unpackedCustomStruct)
} else {
print("no struct retrieved")
}
Depot can store differnet types:
- Structs
- Arrays of structs
- Nested structs
- Enums with raw types
Installation for CocoaPods by adding the following line to your Podfile:
use_frameworks!
pod 'Depot'
To support persisting struct, a struct needs to implement PropertyListReadable
protocol which includes the following functions:
init?(storehouse: Storehousable)
func propertyListRepresentation() -> [String: AnyObject]
init
method that gets each property from the storehouse, and a propertyListRepresentation
method that converts structs to Sictionary that can persisted:
struct Person: PropertyListReadable {
let name: String
let age: Float
let id: Int
init(name: String, age: Float, id: Int) {
self.name = name
self.age = age
self.id = id
}
init?(storehouse: Storehousable) {
self.name = storehouse.read(key: "name") ?? ""
self.age = storehouse.read(key: "age") ?? 25.5
self.id = storehouse.read(key: "id") ?? 22
}
func propertyListRepresentation() -> [String : AnyObject] {
return [ "name": self.name, "age": self.age, "id": self.id ]
}
}
Depot made available under the MIT license.
Depot is brought to you by Sameh Mabrouk