https://docs.swift.org/swift-book/LanguageGuide/Properties.html 原文
摘要
a stored property is a constant or variable that is stored as part of an instance of a particular class or structure.
If you create an instance of a structure and assign that instance to a constant, you cannot modify the instance’s properties, even if they were declared as variable properties
The same is not true for classes, which are reference types. If you assign an instance of a reference type to a constant, you can still change that instance’s variable properties.
A lazy stored property is a property whose initial value is not calculated until the first time it is used.
classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
If a computed property’s setter doesn’t define a name for the new value to be set, a default name of newValue is used
If the entire body of a getter is a single expression, the getter implicitly returns that expression.
You can simplify the declaration of a read-only computed property by removing the get keyword and its braces
Property observers are called every time a property’s value is set,
If you implement a willSet observer, it’s passed the new property value as a constant parameter. You can specify a name for this parameter as part of your willSet implementation. If you don’t write the parameter name and parentheses within your implementation, the parameter is made available with a default parameter name of newValue.
Similarly, if you implement a didSet observer, it’s passed a constant parameter containing the old property value. You can name the parameter or use the default parameter name of oldValue.
When you use a property wrapper, you write the management code once when you define the wrapper, and then reuse that management code by applying it to multiple properties.
关于property wrapper 还需要更多了解。
Global variables are variables that are defined outside of any function, method, closure, or type context. Local variables are variables that are defined within a function, method, or closure context.
You define type properties with the static keyword.
本文深入探讨Swift中的属性概念,包括存储属性、懒加载属性、计算属性及属性观察者等,揭示了它们在结构体和类中的工作原理。同时,文章还讲解了属性包装器的使用,以及全局变量和类型属性的定义。
9134

被折叠的 条评论
为什么被折叠?



