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:

[HTML]View Plaincopy
  1. struct struct body name {①
  2. static VAR (or let) store property = "xxx" ②
  3. ......
  4. Static Var computed property name: property data type {③
  5. get {
  6. Return computed property value
  7. }
  8. Set (new property value) {
  9. ......
  10. }
  11. }
  12. }
  13. Enum enum name {④
  14. static VAR (or let) store property = "xxx" ⑤
  15. ......
  16. Static Var computed property name: property data type {⑥
  17. get {
  18. Return computed property value
  19. }
  20. Set (new property value) {
  21. ......
  22. }
  23. }
  24. }
  25. Class Name {⑦
  26. ......
  27. Class Var computed property Name: property data type {⑧
  28. get {
  29. Return computed property value
  30. }
  31. Set (new property value) {
  32. ......
  33. }
  34. }
  35. }



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.

Tip You cannot access instance properties (including storage properties and computed properties) in a static computed property, but you can access other static properties. 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:

[HTML]View Plaincopy
  1. struct Account {
  2. var amount: Double = 0.0//Account amount
  3. var owner: String = ""//Account name
  4. static var interestrate: Double = 0.668//interest rate ①
  5. static Var staticprop:double {②
  6. return interestrate * 1_000_000
  7. }
  8. var instanceprop:double {③
  9. Return Account.interestrate * Amount
  10. }
  11. }
  12. accessing static properties
  13. println (Account.staticprop) ④
  14. var myAccount = Account ()
  15. accessing instance Properties
  16. Myaccount.amount = 1_000_000⑤
  17. accessing static properties
  18. 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:

[HTML]View Plaincopy
  1. Enum Account {
  2. Case Bank of China ①
  3. Case ICBC
  4. Case China Construction Bank
  5. Case Agricultural Bank of China ②
  6. static var interestrate: Double = 0.668//interest rate ③
  7. static Var staticprop:double {④
  8. return interestrate * 1_000_000
  9. }
  10. var instanceprop:double {⑤
  11. Switch (self) {⑥
  12. Case Bank of China:
  13. account.interestrate = 0.667
  14. Case ICBC:
  15. account.interestrate = 0.669
  16. Case China Construction Bank:
  17. account.interestrate = 0.666
  18. Case Agricultural Bank of China:
  19. account.interestrate = 0.668
  20. }⑦
  21. Return account.interestrate * 1_000_000⑧
  22. }
  23. }
  24. accessing static properties
  25. println (Account.staticprop) ⑨
  26. var myAccount = Account . ICBC
  27. accessing instance Properties
  28. 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:

[HTML]View Plaincopy
  1. Class Account {①
  2. var amount: Double = 0.0//Account amount
  3. var owner: String = ""//Account name
  4. var interestrate: Double = 0.668//interest rate ②
  5. Class var staticprop:double {③
  6. Return 0.668 * 1_000_000
  7. }
  8. var instanceprop:double {④
  9. Return self.interestrate * Self.amount⑤
  10. }
  11. }
  12. accessing static properties
  13. println (Account.staticprop) ⑥
  14. var myAccount = Account ()
  15. accessing instance Properties
  16. Myaccount.amount = 1_000_000
  17. accessing static properties
  18. 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.