//: Playground-noun:a Place where people can playImport UIKit//---property-------////1. Properties of the structstructMyRange {var location:int//Variable PropertiesLet Length:int//Constant Property}//If there is no initial value for a given property, the instantiated struct needs to be constructed using the constructorvar range1 = MyRange (location:0, Length:3) print (range1) range1.location=2//range1.length = 7Let range2= MyRange (Location:3, Length:9)//range2.location = 4//2. Properties of the class//must have an initial valueclassDoor {var color:string="Black"Let width:int=2Let height:int=3var islocked:bool?//If you do not initialize, you can set the property to an optional type//constructors in Swift, similar to the Init initialization method in OCinit () {print ("Door Init")}}var door1=Door ()//print (Door1.color)//print (door1.islocked)//3. Delay Storage properties, lazy loadingclassImageData {var fileName="Image.png"init () {//read a picture of the local name filename//larger picture, more time consumingPrint"ImageData init method: Read the data from the picture file") } }classDataManager {var data=[String] () lazy var image=ImageData () init () {print ("DataManager Init method")}}let DataManager=DataManager () dataManager.image.fileName//It is not necessary to construct an image instance when creating DataManager, but to create it when using the image instance to improve the execution efficiency of the program. //The lazy Store property must be declared as a variable property (VAR) because its initial value is not retrieved until the instance is initialized. //The constant attribute (let) should be assigned before the instance initialization is complete, so the constant attribute cannot be declared as lazy store property. //4. Calculating Properties//Store Properties: Stores the values of constants or variables as part of an instance. //Computed properties: Only the value of the computed property, not just the storage. //computed properties can be used for classes, structs, enumerations, and stored properties can only be used with classes and structs. //class Student {// //var name:string?// //var age:int {// // //Getter Method//get {//return ten// }// // //Setter Method//Set (newage) {// //NewAge is the value of the new setting//Print ("newage=\ (newage)")// // }// // // }// //}////var stu = Student ()//Stu.age = Ten//print (stu.age)structPoint {var x=0.0var y=0.0}structSize {var width=0.0var height=0.0}structRect {var origin=Point () var size=Size ()//This property can be calculated directly, without the need for additional storagevar center:point {Get{Let CenterX= origin.x + size.width/2Let centery= ORIGIN.Y + size.height/2 returnPoint (X:centerx, Y:centery)}//The set method of a computed property has an implicit formal parameter named NewValue, which represents the new value passed in. Set { //change the coordinates of the starting point by the new center point coordinate, and finally achieve the purpose of changing the rect display position.origin.x = newvalue.x-size.width/2ORIGIN.Y= newvalue.y-size.height/2}}}var point1:point=Point () var size1:size= Size (width: -, Height: -) var rect1=Rect (Origin:point1, Size:size1) var center1=rect1.centerprint (center1)//get the value to center by calculating the property's Get methodcenter1.x= -center1.y= -Rect1.center= Center1//Call setter Methodprint (Rect1.origin)//computed properties do not store data, only interfaces that provide access to and set data//only getter, no setter, read-only computed property is provided//only setter is provided, no getter is provided, syntax is not allowedstructCube {var width=0.0var height=0.0var depth=0.0var cul:double {//get {//The Get keyword can be omitted when the computed property is read-only returnWidth * Height *Depth//}}}var Cube= Cube (width:Ten, Height:Ten, Depth:Ten)//Cube.cul = 10000//5. Attribute observer, similar to KVO in OC//Swift provides two methods Willset and Didset, which can listen for changes in property valuesclassStepcounter {var totalcounts:int= - { //This property value is set before it is calledWillset {//can not change the value of the listening property in the Willset, easy to cause a dead loop//totalcounts + = newvalue;Print"new value for Totalvalue =\ (newvalue)") } //This property value is set immediately after the callDidset {//You can change the value of a listener's property in Didset because it is not called until the property value is set .Totalcounts + =OldValue; Print ("old value of Totalvalue =\ (oldValue)") } //If you do not specify parameters for Willset and Didset, the system generates a set of newvalue representing the new value by default, OldValue represents the old value}}let Step=stepcounter () step.totalcounts= $
Swift struct and class properties -005-swift class struct Basic properties