first, the Prelude
Swift as a new language, after several years of development, gradually improved, has now been updated to 3.0 version, it brings together many other language features, such as JS, Paython, etc., completely different from OC. The individual feels that it does not have the full OOP and ood nature.
Two, talk about the difference of several attributesStore Properties
Definition: The actual storage of constants and variables
Calculated Properties
Definition: Dependent on the storage property, calculated by providing getter access values, providing setter methods to indirectly set values for other properties or variables
Class Properties
Definition: Its essence is actually a global attribute, which bounds its scope in the class, and modifies it with the keyword static
Lazy Load Properties
Definition: This property is initialized once when used, is modified with the keyword lazy, must be initialized, non-instance property lazy loading after curly braces {} to add ()
Global Properties
Definition: Properties outside the class, scope global. Properties similar to the static modifier of OC
classViewcontroller:uiviewcontroller {override func Viewdidload () {super.viewdidload () //Instance Properties Let P=Person ()
//Storage Properties Print(P.age)
//Lazy Load Properties Print(P.name)
//class properties Print(Person.height)
//Computed properties Print(P.getage)
//Global Properties Print(Commonproperty)}}// global attribute var commonproperty= {()->stringinch Print("Common Property1") return "Common Property" }()classperson{// Class Property static var height= {()->intinch Print("Static Properties") return170 }() // Storage Property var age= {()->intinch Print("Store Properties") return26 }() // calculate attribute var getage:int{get{Print("Computed Properties") returnAge }} // Lazy Load Property lazy var name= {()->stringinch Print("Lazy Properties") return "Lazy Liyang" }() // Constructor Method init () {Print("Init") }}
Three, summary: Based on class, class construction
- Store property, initialized first
- Construction method, second only to storage property invocation, where you can assign a value to a stored property
- Lazy-load properties, class properties, and global properties are initialized at the first use, and subsequent calls are not initialized
- Warning: When lazy-loading properties are calculated based on a storage attribute, do not use lazy-load properties, calculated properties
Swift: Talk about the differences between several common swift properties