Persisted.swift 624 B

1234567891011121314151617181920
  1. import Foundation
  2. /// Attention! Do not use this wrapper for mutating structure with `didSet` handler into property owner!
  3. /// `didSet` will never called if structure mutate into itself (by "mutating functions").
  4. @propertyWrapper
  5. struct Persisted<Value: Codable> {
  6. var wrappedValue: Value? {
  7. set { storage.setValue(newValue, forKey: key) }
  8. get { storage.getValue(Value.self, forKey: key) }
  9. }
  10. private let key: String
  11. private let storage: KeyValueStorage
  12. init(key: String, storage: KeyValueStorage = UserDefaults.standard) {
  13. self.storage = storage
  14. self.key = key
  15. }
  16. }