1. Comparing attributes in Java,swift is interesting, there are many categories: storage properties, computed properties, type attributes, and so on.
2. Storage Properties
Storage properties: A stored property is a constant or variable stored in an instance of a particular class or struct, which can be a variable store property (defined with a keyword var
) or a constant storage attribute (defined with a keyword let
).
If you create an instance of a struct and assign a value to a constant, you cannot modify any of the properties of the instance, even if the variable store property is defined, the code is as follows:
0 4 )// This interval represents the integer 0,1,2,3rangeoffouritems. Firstvalue 6 // even though firstvalue is a variable attribute, there will be an error .
2.1 Deferred Storage properties
A deferred store property is a property whose initial value is evaluated when it is first called. Used before the property declaration lazy
to indicate a deferred store property. You must declare the deferred store property as a variable (using the var
keyword), because the value of the property may not be available until the instance construction is complete. The constant property must have an initial value before the construction process is complete, and therefore cannot be declared as a deferred attribute.
When the value of a property depends on external factors that do not know the value before the end of the instance's construction, or when the value of the property requires complex or large computations, it can be computed only when needed, and we can use the deferred store property. The code is as follows:
class dataimporter { "data.txt" } class DataManager { = dataimporter () = [String] () } = DataManager () Manager.data.append (" new data")
The keyword in front of imprter is lazy, is a deferred store property, DataManager
or may not import data from a file. So when an DataManager
instance is created, there is no need to create an DataImporter
instance, and it is wiser to DataImporter
create it when it is used.
2.2 Calculation Properties
In addition to storing properties, classes, structs, and enumerations can define computed properties , calculated properties do not store values directly, but instead provide a getter to get the value, an optional setter to indirectly set the value of another property or variable. The code is as follows:
structPoint {var x=0.0, y =0.0 } structSize {var width=0.0, height =0.0 } structRec {var origin=Point () var size=Size () var center:point {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)}}}var Square= Rec (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) Print ("Square.origin is now at (\ (square.origin.x), \ (SQUARE.ORIGIN.Y))")
Rec
Provides a center
computed property named. Rec
the computed properties center
provide a custom getter and setter to get and set the center point of the rectangle as if it had a stored property.
2.3 Read-only computed properties
Only a getter without a setter is a computed property that is a read-only computed property. A read-only computed property always returns a value that can be accessed through the point operator, but cannot set a new value. The code is as follows:
structCuboid {var width=0.0, height =0.0, depth =0.0var volume:double {returnWidth * Height *Depth}} Let Fourbyfivebytwo= Cuboid (width:4.0, Height:5.0, Depth:2.0) Print ("The volume of Fourbyfivebytwo is \ (fourbyfivebytwo.volume)")//output "The volume of Fourbyfivebytwo is 40.0"//the declaration of a read-only computed property can be stripped of the Get keyword and braces
3. Property Watcher
Property observers Monitor and respond to changes in property values, and each time a property is set to a value, the property observer is called, even if the new value is the same as the current value. You can add one or all of the following observers for a property:
willSet
Called before a new value is set
didSet
Called immediately after a new value has been set
Look at the following section of code:
classStepcounter {var totalsteps:int=0{Willset (newtotalsteps) {print ("About -to-set totalsteps to \ (newtotalsteps)")} didset {ifTotalsteps >oldValue {print ("Added \ (totalsteps-oldvalue) steps")}}}}let Stepcounter=stepcounter () stepcounter.totalsteps= $//About-to- set totalsteps to//Added StepsStepcounter.totalsteps = the//About-to- set totalsteps to//Added StepsStepcounter.totalsteps =896//About-to- set Totalsteps to 896//Added 536 Steps
4. Type properties
The properties of an instance belong to a particular type instance, each of which has its own set of property values each time the type is instantiated, and the attributes between the instances are independent of each other. You can also define properties for the type itself, which have only one copy, regardless of the number of instances of the type. This property is the type attribute . A stored Type property of a value type can be a variable or constant, and a computed type property is defined as a variable property as the computed property of an instance.
4.1 Type attribute Syntax
Use keywords static
to define type properties for value types, and class to define type properties. The following example shows the syntax for a stored and computed type property, with the following code:
structSomestructure {Staticvar storedtypeproperty ="Some value." Staticvar computedtypeproperty:int {//This returns an INT value }}enumsomeenumeration {Staticvar storedtypeproperty ="Some value." Staticvar computedtypeproperty:int {//This returns an INT value }}classSomeClass {
Static// returns an Int value }}
4.2 Getting and setting the value of a Type property
Like the properties of an instance, access to a Type property is done through the dot operator, but the Type property is obtained and set by the type itself, not by the instance. Like what:
print (Someclass.computedtypeproperty) // output " a" print (Somestructure.storedtypeproperty) // output "Some value." " another value. " print (Somestructure.storedtypeproperty) // output "another value."
Properties in Swift