Swift Static properties

Source: Internet
Author: User

before we introduce static properties, let's look at the design of a class that has an account (bank accounts) class, assuming it has 3 attributes: Amount (account amount), interestrate (interest rate), and owner (account name). Of these 3 attributes, amount and owner will vary from one account to another, and the contents of each account are different, and the interestrate of all accounts are the same.
The amount and owner properties are related to the account entity, called instance properties. The InterestRate attribute is independent of the individual, or shared by all account individuals, which is called a static property or type attribute.
There are 3 object-oriented types (structs, enumerations, and classes) that can define static properties, and their syntax formats are as follows:
struct struct name {               ①    static var (or let) store property  = "xxx"               ②    ... Static Var computed property name: property data type {       ③    get {               return computed property value      }    Set (new property value) {          ...    }    } Enum enum name {   ④    static var (or let) store property  = "xxx"              ⑤    ... Static Var computed property name: property data type {       ⑥    get {           return computed property value        }        Set (new property value) {        ...    }         } Class Name {      ⑦ ...    Class Var computed property Name: property data type {        ⑧    get {        return computed property value    }       Set (new property value) {          ...    }      }


In the preceding code, line ① is the definition of a struct, where static storage properties and computed properties can be defined. The ② code defines a static storage property, and the Declaration keyword is static, which can be a variable property or a constant property. The ③ line code defines a static computed property, declares that the keyword used is static, the computed property cannot be a constant, and only the variable. struct static computed properties can also be read-only, with the following syntax:
Static Var computed property name: property data type {
Return computed property value
}
The ④ is defined as an enumeration in which an instance store property cannot be defined, but static storage properties can be defined, or static computed properties can be defined. Defining enumeration static properties is exactly the same syntax as defining a static property of a struct, which is not discussed here.
Line ⑦ is a definition class in which you can define instance store properties, but you cannot define static storage properties. A static computed property can be defined in a class. The keyword used by the declaration is class, which differs from the declaration of structs and enumerations.
We summarize the above instructions, as shown in the table below.
TipsInstance properties, including stored and computed properties, cannot be accessed in a static computed property, but other static properties can be accessed. Instance properties can be accessed in the instance calculation properties, and static properties can also be accessed.
Static property of structure body
Let's look at an account structure static Property Example:
struct Account {        var amount:double = 0.0                 //Accounts amount    var owner:string = ""                   //Account name        static var Interestrat e:double = 0.668  //interest rate ①    static var staticprop:double {②        return interestrate * 1_000_000    }        var Insta nceprop:double {③        return account.interestrate * Amount    }}//access static properties println (Account.staticprop) ④var MyAccount = account ()//Access Instance Property Myaccount.amount = 1_000_000⑤//Access static property println (Myaccount.instanceprop) ⑥


The code above defines the account structure, where the ① line code defines the static storage property interestrate, and the ② line code defines the static computed property Staticprop, which can access static properties such as InterestRate in its property body. The ③ code defines the instance calculation property Instanceprop, which can access the static property interestrate in its property body, accessed in the form "type name. Static property", such as Account.interestrate. The ④ Line code is also an Access static property, accessed in the same way as "type name. Static Property".
The ⑤ line and the ⑥ line code are access instance properties, accessed by "instance. Instance Properties".
Two Enumerate static properties
Let's look at an account enumeration static Property Example:
Enum Account {case        BOC ① case ICBC Case China    Construction Bank case    ABC ②        static var interestrate:double = 0.66 8//Interest rate ③        static var staticprop:double {④        return interestrate * 1_000_000    }        var instanceprop:double {⑤
   switch (self) {⑥ case            Bank of China:                account.interestrate = 0.667 case            ICBC:                account.interestrate = 0.669 Case China            Construction Bank:                account.interestrate = 0.666 Case            Agricultural Bank of China:                account.interestrate = 0.668        } ⑦        return account.interestrate * 1_000_000⑧    }}//Access static properties println (Account.staticprop) ⑨var MyAccount = Account. ICBC//Access Instance Properties println (Myaccount.instanceprop) ⑩


The code above defines the account enumeration type, where the ①~② line code defines the 4 members of the enumeration. The ③ code defines the static storage property interestrate, and the ④ line code defines the static computed property Staticprop, which can access static properties such as InterestRate in its property body. The ⑤ code defines the instance calculation property Instanceprop, where the ⑥~⑦ line code uses the switch statement to determine the value of the current instance, to obtain different interest, and the ⑥ line code uses self, which refers to the current instance itself. The ⑧ Line code is the result of the return calculation.
The ⑨ line code is the Access static property. The ⑩ line code is the Access instance property.
The results of the sample run are as follows:
668000.0
669000.0
Third, class static properties
Let's look at an account class static Property Example:
Class Account {①        var amount:double = 0.0               //Amount    var owner:string = ""                 //Account name        var interestrate:d ouble = 0.668       //Interest rate ②    class var staticprop:double {③        return 0.668 * 1_000_000    }        var instanceprop:do uble {④        return self.interestrate * Self.amount⑤    }}//Access static properties println (Account.staticprop) ⑥var MyAccount = Account ()//Access Instance Property Myaccount.amount = 1_000_000//Access static property println (Myaccount.instanceprop) ⑦


The above Code section ① defines the account class, and the ② line code defines the storage property interestrate, noting that static storage properties cannot be defined in the class. The ③ code defines the static computed property Staticprop, which is the class keyword. The ④ code defines the instance calculation property Instanceprop, which accesses the instance properties InterestRate and amount in the ⑤ line code, accessing the instance properties of the current object can be added "self" in front of the property. The ⑥ Line code is also an Access static property. The ⑦ line code is the Access instance property.



For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485

Welcome to Luxgen iOS Classroom public Platform


Swift Static 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.