文章标题 原创 翻译 转载 文章内容 ``` protocol ModelDelegate { func willValueChanged(curValue: Int, newValue: Int) func didValueChanged(curValue: Int, oldValue: Int) } class Model { var delegate: ModelDelegate? var count: Int = 0 { willSet { delegate?.willValueChanged(count, newValue: newValue) } didSet { delegate?.didValueChanged(count, oldValue: oldValue) } } } class View: ModelDelegate { func willValueChanged(curValue: Int, newValue: Int) { print("will, cur:\(curValue), new:\(newValue)") } func didValueChanged(curValue: Int, oldValue: Int) { print("did, cur:\(curValue), old:\(oldValue)") } } let model = Model() let view = View() model.delegate = view model.count = 100 model.count = 200 ``` 文章类别 Python Mobile Android Java Shell Life Database Bug Windows IOS Tools Boost Node.js Mac Product Tips C/C++ Golang Javascript React Qt MQ MongoDB Design Web Linux LLM ChatGPT RAG AI 提交