//: Playground-noun:a Place where people can playImport UIKit//----class and struct------////1. Definition of structurestructSize {//properties in the struct, given default valuesvar width =375var height=667 //methods can be defined in Swift's structure, unlike C, OCfunc Show () {print ("(\ (width): \ (height))")}}var size= Size ()//Construction of a struct to construct an instance of a struct bodyprint (size.width) print (size.height) size.show ()//2. Definition of ClassclassPerson {//the properties defined in the class must be initializedvar name:string =""var age:int=0Let sex:int=0 //definition of a method in a classFunc speak () {print ("Say hello name:\ (name), age:\ (age), sex:\ (Sex)") } }//Create an instanceLet Kathy =Person () kathy.speak () Kathy.name="Kathy"Kathy.age=Ten//kathy.sex = 1Kathy.speak ()//3. A property in a class can be a struct-body variableclassView {var opacity=1.0var name=""var viewsize=Size ()}var MyView=View ()//Swift allows setting child properties of struct properties in a classMyView.viewSize.width =480//4. The difference between a struct and a class//(1) The structure has a corresponding one-through constructorvar size2 = Size (width: the, Height: -) size2.show ()//and there is no corresponding constructor in the classvar myView2 =View ()//(2) structs and enumerations are value types, and classes are reference typesvar size3 =Size2size2.width=480size2.show () size3.show () var mark=Person () Mark=Kathy//Mark.speak ()//Kathy.speak ()Kathy.age = -Kathy.speak () mark.speak ( )//The memory management of the instance of the class still uses the Arc//5.=== operator: Used to determine whether a variable or constant refers to the same instance, only for reference types//!== operatorifMark = =Kathy {print ("referencing the same instance")}//= = = and ==,== The value of two instances is equal, = = = Whether the same instance is referenced
Swift class and struct 004-class and struct basic use