Summary of Knowledge points:
1. Storage Properties
struct town{ "South"// read-only attribute 5422// read/write Properties }
2. Lazy Storage Properties
For properties, lazy loading means that the value of the property only appears the first time it is accessed, so the lazy property must be declared as Var. Note: properties marked as lazy will only be evaluated once.
structtown{ Let region=" South" //read-only propertiesvar population =5422 //read/write Properties enumSize { CaseSmall CaseMiddle CaseLarge} lazy var townsize:size= { Switchself.population { Case 0...10000: returnSize.small Case 10001...100000: returnSize.middledefault: returnSize.large}} ()}
EXPLANATION Two points: 1, the self importance of self.population: this closure must refer to self in order to access the instance's population property within the closure, 2, why this place uses the lazy attribute: In order to let the closure can safely access self, The compiler must know that self has been initialized to completion. Marking townsize as Lazy tells the compiler that this property is not necessary to create self, and if it does not exist, it should be created when it is first accessed. This tells the compiler that self is definitely available when the closure is called.
3. Calculation properties
Read-only computed properties
struct town{ 2300}class monster{ "Monster" var town:town ? var victimpool:int{ get{ return0 } } }
Read-Write computed properties
struct town{ 2300}class monster{ "Monster" var town:town ? var victimpool:int{ get{ return0 } Set{ town?. Population = newvalue } }}
4. Attribute Observer
Property observers are available for any custom store properties and any inherited properties. Custom computed properties cannot be observed with attributes
struct town{ 2300 { willset{ print ("Thepopulation would change from\ ( population) to \ (newvalue) } didset{ print ("the Population have changed from \ (OldValue) to \ (population)}}}
5. Type attribute
Type properties are common to types, and their values are shared between instances of the same type. Value types (structs and enumerations) can have either a storage type property or a computed Type property, and the Type property of a value type begins with the keyword static. Classes can also have storage-type properties and computed-type properties, which, if used with static syntax, cannot overwrite the class properties of the parent class, and if the class keyword is used, the subclass can provide its own implementation for one of the classes ' properties.
Properties of Swift