可以持久化基礎類型變量值的類庫。
你可以存儲以下類型
基礎類型(String, Int, Float, Bool)
if let available: Bool = Pantry.unpack("promptAvailable") { completion(available: available) } else { anExpensiveOperationToDetermineAvailability({ (available) -> () in Pantry.pack(available, key: "promptAvailable", expires: .Seconds(60 * 10)) completion(available: available) }) }自動的持久化變量
var autopersist: String? { set { if let newValue = newValue { Pantry.pack(newValue, key: "autopersist") } } get { return Pantry.unpack("autopersist") } } ...later... autopersist = "Hello!" // restart app, reboot phone, etc print(autopersist) // Hello!結構體
struct Basic: Storable { let name: String let age: Float let number: Int init(warehouse: JSONWarehouse) { self.name = warehouse.get("name") ?? "default" self.age = warehouse.get("age") ?? 20.5 self.number = warehouse.get("number") ?? 10 } }類
class ModelBase: Storable { let id: String required init(warehouse: Warehouseable) { self.id = warehouse.get("id") ?? "default_id" } } class BasicClassModel: ModelBase { let name: String let age: Float let number: Int required init(warehouse: Warehouseable) { self.name = warehouse.get("name") ?? "default" self.age = warehouse.get("age") ?? 20.5 self.number = warehouse.get("number") ?? 10 super.init(warehouse: warehouse) } }