当前位置:网站首页>Swift 中监听属性值变化 (观察者模式)
Swift 中监听属性值变化 (观察者模式)
2022-07-20 22:23:00 【hello_Jesse】
很简单,直接上代码,一看就会
class Test3ViewController: BaseViewController {
lazy var person:Person = {
let p = Person()
return p
}()
var observer:NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
person.name = "jack"
observer = person.observe(\Person.name, options: [.old,.new], changeHandler: {
person,change in
if let old = change.oldValue {
print("old == \(old)") //old == jack
}
if let new = change.newValue {
print("new == \(new)")//new == rose
}
})
person.name = "rose"
}
}
class Person:NSObject {
@objc dynamic var name = ""
}
边栏推荐
- 打通了!Jira + Zadig 實現需求與研發過程追踪
- Summary of APP page seconds open optimization~
- It centralized purchase of Postal Savings Bank of China: servers, arrays, switches, routers, firewalls, etc
- Beisen prospectus: the advantages of the track are prominent, and integration + medium and large customers are plus points
- iptables详解
- 百度工程师眼中的云原生可观测性追踪技术
- 全国职业院校技能大赛网络安全B模块 “web安全之综合渗透测试”
- Installing C3d v1.0
- 39岁被裁+背房贷(没活路了)
- 2.5 亿、巴南区感知体系数据采集、后台分析、雪亮工程网络及运维服务项目:中国电信中标
猜你喜欢
随机推荐
6.1-wy16 no two
39 year old layoff + mortgage (no way to live)
《强化学习周刊》第54期:i-Sim2Real、GriddlyJS & 逆强化学习(IRL)最新应用
【保研】-- 保研夏令营中英语口语技巧问题
【保研】-- 整理分享一下之前学校复试面试存在的问题
满街皆是圣贤是可能的
RKE vs. RKE2:对比两种 Kubernetes 发行版
Reinforcement learning weekly 54: i-sim2real, griddlyjs & the latest application of inverse reinforcement learning (IRL)
Part 131 erc20 lock up contract
[Baoyan] - interview precautions on the middle line of Baoyan summer camp
c# winform 实际操作XML代码,包括创建、保存、查询、删除窗体演示
[leetcode] use bisection in unordered arrays
云图说丨数字资产链:您的数字资产产权保护神
APP 页面秒开优化方面总结~
Beisen prospectus: the advantages of the track are prominent, and integration + medium and large customers are plus points
ABAP(ALV部分)
前 3 名突然变了,揭秘 7 月编程语言最新排行榜
7.2-cm46 legal bracket sequence judgment
RS-485 communication
JS optional chain