当前位置:网站首页>SwiftUI Swift 内功之 Swift 中的属性观察者 didSet 与 willSet
SwiftUI Swift 内功之 Swift 中的属性观察者 didSet 与 willSet
2022-06-24 09:51:00 【知识大胖】
“在 Swift 中,您可以将属性观察器附加到变量上,以便在变量更改时运行代码。”
属性观察者,持续观察属性值,在属性值改变之前或之后触发。有两种类型的属性观察者;didSet和willSet。
- didSet:当该值刚刚被设置时,则调用该观察者并执行本节中的代码。
- willSet:在设置属性值之前调用本节中的代码。
请注意,使用属性观察器有一些限制。
“属性观察者被声明为变量而不是常量,因为它只是一个可以被属性观察者跟踪的可变属性。因此,属性观察者是用var而不是let关键字声明的。”
class Observable {
static var someProperty: String? {
willSet {
print("Some property will be set.")
}
didSet {
print("Some property has been set.")
}
}
}
- “一个属性必须有一个初始值,比如nil。”
- “你不能使用带有计算属性的属性观察器。这是因为计算属性不存储值。”
- “你不能将属性观察者附加到惰性变量上。惰性变量在第一次被调用时被初始化。惰性变量没有与第一个限制相矛盾的初始值。”
我们为什么要使用它们?方便,当然。
让我们的例子更容易理解。
var name: String? {
willSet {
边栏推荐
- Charles packet capturing tool tutorial
- Why should we make the best use of the external chain in SEO?
- Canvas infinite scan JS special effect code
- What is the resource search platform and how resource search works
- Detailed explanation of SQL Sever basic data types
- JMeter interface test tool foundation - badboy tool
- [latest in the whole network] how to start the opentsdb source code in the local ide run
- 抓包工具charles實踐分享
- A method to solve the self-adaptive width and height of the internal picture of rich text label in wechat applet
- 2022 International Symposium on intelligent robots and systems (isoirs 2022)
猜你喜欢

Practice sharing of packet capturing tool Charles

Quick completion guide for manipulator (III): mechanical structure of manipulator

程序员大部分时间不是写代码,而是。。。

24. image mosaic operation

Distributed transaction principle and solution

Fais ce que tu veux.
![[JS reverse sharing] community information of a website](/img/71/8b77c6d229b1a8301a55dada08b74f.png)
[JS reverse sharing] community information of a website
![[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)

【本周六活动】.NET Day in China

Hbuilder makes hero skin lottery games
随机推荐
Smart energy: scenario application of intelligent security monitoring technology easycvr in the petroleum energy industry
使用Process Monitor工具监测进程对注册表和文件的操作
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
Virtual CD-ROM function how to use and install virtual CD-ROM
Several stacks of technology sharing: product managers' Online Official answers to several stacks of knowledge
Act as you like
js数组求和的5种方法
Suddenly I thought of the wooden house in my hometown
H5 video conference, camera monitoring, web streaming and live broadcast integration scheme
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
Illustration miscellaneous [for archiving to prevent loss]
Lightweight deployment of firefoxsend temporary file sharing service using Tencent cloud
[Qianfan 618 countdown!] IAAs operation and maintenance special preferential activities
Tencent's open source project "Yinglong" has become a top-level project of Apache: the former long-term service wechat payment can hold a million billion level of data stream processing
Differences among cookies, session, localstorage and sessionstorage
Many of my friends asked me what books and online classes I recommended. This time, I contributed all the materials that I had been hiding for a long time (Part 1)
Cross domain overview, simple accumulation
Introduction to the use of splice() method
Svg+js drag slider round progress bar
Base64 decoding method three ways for non professionals