Issue #465
Use EasyStash
import EasyStash
final class Store<T: Codable & ItemProtocol>: Codable {
var items = [T]()
func bookmark(item: T) {
items.append(item)
}
func unbookmark(item: T) {
guard let index = items.firstIndex(where: { $0.itemId == item.itemId }) else {
return
}
items.remove(at: index)
}
func isBookmark(item: T) -> Bool {
return items.contains(where: { $0.itemId == item.itemId })
}
}
import EasyStash
final class StoreContainer {
var food: Store<Food>
static var shared = StoreContainer()
let storage = try! Storage(options: Options())
init() {
food = try? storage.load(forKey: "food", as: Store<Food>.self) ?? Store<Food>()
}
}
If Swift has problem compiling because of generic, use try catch to declare in multiple steps in stead of ??
init() {
do {
self.food = try storage.load(forKey: "food", as: Store<Food>.self)
} catch {
self.food = Store<Food>()
}
}