没有combine处理响应式编程
1. KVO(Key-Value Observing)
class MyModel: NSObject {
@objc dynamic var value: String = ""
}
class MyViewController: UIViewController {
var model = MyModel()
var observation: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
// 使用 KVO 监听 value 属性的变化
observation = model.observe(\.value, options: [.new]) { object, change in
if let newValue = change.newValue {
print("Value changed to \(newValue)")
}
}
// 模拟属性变化
model.value = "New Value"
}
}2. 代理模式(Delegate)
3. NotificationCenter(通知中心)
4. 第三方响应式框架(如 RxSwift)
5. 状态管理(例如 @State, @Binding 和 @ObservedObject)
@State, @Binding 和 @ObservedObject)总结
Last updated