我正在尝试使用@propertyWrapper
. 我想做的是为我的应用用户偏好创建一个包装类。所以我写了以下代码。
@propertyWrapper
struct Storage<T> {
private let key: String
private let defaultValue: T
var projectedValue: Storage<T> { return self }
var wrappedValue: T {
get {
return UserDefaults.standard.string(forKey: key) as? T ?? defaultValue
}
set {
UserDefaults.standard.set(newValue, forKey: key)
}
}
init(key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
func observe(change: @escaping (T?, T?) -> Void) -> NSObject {
return DefaultsObservation(key: key) { old, new in
change(old as? T, new as? T)
}
}
}
然后,我想观察 UserDefaults 值的变化。所以我实现了一个名为DefaultsObservation
.
class DefaultsObservation: NSObject {
let key: String
private var onChange: (Any, Any) -> Void
init(key: String, onChange: @escaping (Any, Any) -> Void) {
self.onChange = onChange
self.key = key
super.init()
UserDefaults.standard.addObserver(self, forKeyPath: key, options: [.old, .new], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
guard let change = change, object != nil, keyPath == key else { return }
onChange(change[.oldKey] as Any, change[.newKey] as Any)
}
deinit {
UserDefaults.standard.removeObserver(self, forKeyPath: key, context: nil)
}
}
我的 AppData 课程也在关注。
struct AppData {
@Storage(key: "layout_key", defaultValue: "list")
static var layout: String
}
但是,当我尝试添加和监听更改layout
属性时,它无法正常工作。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppData.$layout.observe { old, new in
print(old)
}
}
当我调试它时,deinit
只要viewWillAppear
调用方法就可以工作。当我注释掉deinit
删除观察者的方法时,一切都很完美。我认为关闭deinit
会导致一些内存问题。所以我不想评论它。我错过了什么,我该如何解决?