1. Stored Properties
1.1 Overview
classes, structures, and enumerations can all define Stored Properties
Stored Properties This is the simplest type of attribute. For example:
struct Fixedlengthrange { var firstvalue:int let length:int } var 0 3 ) // the range represents integer values 0, 1, and 2 6 The range now represents integer values 6, 7, and 8
Where length is a let type and cannot be changed once initialized.
1.2. Stored properties of a constant struct instance (Stored properties of Constant Structure Instances)
If you create an instance of a struct and assign it to a constant constant, you cannot modify the value of this property even if the struct's property is a variable type.
0 4 ) // This range represents integer values 0, 1, 2, and 3 6 // This would report an error, even though firstvalue are a variable property
In the above code, the value of the Rangeoffouritems property cannot be modified.
Because a struct is a value type, if the struct is constant, all of his properties are also represented as constant.
This is different from the class, because the class is a reference type, and if the instance of a class is constant, you can still change the value of the property of this instance variable type.
1.3. Lazy loading of attributes lazy Stored properties
Lazy Stored Properties means that the initialization value of a property is not calculated immediately, but only when it is used for the first time.
When you need to indicate that a property uses lazy loading, you can add lazy
keywords to the definition.
Note : Lazy-load properties are only available for variables of type Var, because it may not be initialized immediately. However, a let type cannot use lazy loading because it must have a value when it is defined.
classDataimporter {//assume that the class performs time-consuming operations varFileName ="Data.txt" } classDataManager { lazy varImporter =Dataimporter ()vardata =[String] ()//The DataManager class would provide data management functionality here} let manager=DataManager () manager.data.append ("Some Data") Manager.data.append ("Some More Data") //The Dataimporter instance for the Importer property had not yet been created
In the above example, the Dataimporter instance is created only if the Importer property is first used by the is accessed.
println (manager.importer.fileName) // The Dataimporter instance for the importer Property have now been created // prints "data.txt"
2. Computed Properties
2.1 Overview
classes, structures, and enumerations can all define computed properties.
computed properties does not really store a value, instead it provides a getter and an optional setter that indirectly retrieves and sets the other properties and values of retrieve and set.
structPoint {varx =0.0, y =0.0 } structSize {varwidth =0.0, height =0.0 } structRect {varOrigin = Point ()//Stored Property varSize =Size ()varCenter:point {//Computed Property Get{Let CenterX= origin.x + (Size.width/2) Let CenterY= ORIGIN.Y + (Size.Height/2) returnPoint (X:centerx, Y:centery)}Set(newcenter) {origin.x= Newcenter.x-(Size.width/2) ORIGIN.Y= Newcenter.y-(Size.Height/2) }}}varSquare = Rect (Origin:point (x:0.0Y:0.0), Size:size (width:10.0, Height:10.0)) let Initialsquarecenter=Square.center Square.center= Point (x:15.0Y:15.0) println ("Square.origin is now at (\ (square.origin.x), \ (SQUARE.ORIGIN.Y))") //Prints "Square.origin is today at (10.0, 10.0)"
2.2 Shorthand setter definition shorthand setter Declaration
Ix. Attribute Properties