Swift's properties

Source: Internet
Author: User
Tags closure

Summary of Knowledge points:

1. Storage Properties

struct town{    "South"// read-only attribute    5422// read/write Properties }

2. Lazy Storage Properties

For properties, lazy loading means that the value of the property only appears the first time it is accessed, so the lazy property must be declared as Var. Note: properties marked as lazy will only be evaluated once.

structtown{ Let region=" South" //read-only propertiesvar population =5422 //read/write Properties        enumSize { CaseSmall CaseMiddle CaseLarge} lazy var townsize:size= {        Switchself.population { Case 0...10000:            returnSize.small Case 10001...100000:            returnSize.middledefault:            returnSize.large}} ()}

EXPLANATION Two points: 1, the self importance of self.population: this closure must refer to self in order to access the instance's population property within the closure, 2, why this place uses the lazy attribute: In order to let the closure can safely access self, The compiler must know that self has been initialized to completion. Marking townsize as Lazy tells the compiler that this property is not necessary to create self, and if it does not exist, it should be created when it is first accessed. This tells the compiler that self is definitely available when the closure is called.

3. Calculation properties

Read-only computed properties

struct town{    2300}class  monster{    "Monster"      var town:town ?    var victimpool:int{        get{            return0        }    }    }

Read-Write computed properties

struct town{    2300}class  monster{    "Monster"      var town:town ?    var victimpool:int{        get{            return0        }         Set{            town?. Population = newvalue        }     }}

4. Attribute Observer

Property observers are available for any custom store properties and any inherited properties. Custom computed properties cannot be observed with attributes

struct town{    2300  {        willset{            print ("Thepopulation would change from\ ( population) to \ (newvalue)        }        didset{            print ("the Population have changed from \ (OldValue) to \ (population)}}}    

5. Type attribute

Type properties are common to types, and their values are shared between instances of the same type. Value types (structs and enumerations) can have either a storage type property or a computed Type property, and the Type property of a value type begins with the keyword static. Classes can also have storage-type properties and computed-type properties, which, if used with static syntax, cannot overwrite the class properties of the parent class, and if the class keyword is used, the subclass can provide its own implementation for one of the classes ' properties.

Properties of Swift

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.