Swift: Talk about the differences between several common swift properties

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.