Swift static method

Source: Internet
Author: User
Tags instance method

Similar to static properties, a static method, also known as a type method, is defined in Swift, which refers to enumerations, structs, and classes. A static method defines a method that is similar to a static property, where the keyword used by the enumeration and static methods of the struct is static, and the keyword used by the static method of the class is class.
A static method in a structural body
Let's look at an example of a struct static method, with the following code:

[HTML]View Plaincopy print?
  1. struct Account {
  2. var owner: String = "Tony"//Account name ①
  3. static var interestrate: Double = 0.668//interest rate ②
  4. static func Interestby (amount:double)-> double {③
  5. Return InterestRate * Amount
  6. }
  7. Func Messagewith (amount:double)-> String {④
  8. var interest = Account.interestby (amount)
  9. Return "\ (self.owner) interest is \ (Interest)"
  10. }
  11. }
  12. Calling a static method
  13. println (Account.interestby (10_000.00)) ⑤
  14. var myAccount = Account () ⑥
  15. Invoking instance methods
  16. println (Myaccount.messagewith (10_000.00)) ⑦



The code above is defining the account structure, and the ① code declares the instance property owner. The ② line code declares the static property interestrate. The ③ line code defines a static method Interestby, which, like static computed properties, cannot access instance properties or instance methods.
Line ④ is the definition of the instance method Messagewith, the instance method can access the instance properties and methods, and also can access the static properties and methods. In this method we use the Self.owner statement, where self is a hidden property that refers to the current type instance, and generally we do not use it unless the property name conflicts with the variable or constant name.
It is also possible to use self in a static method of Swift, which is not allowed in other object-oriented computer languages. At this point, self represents the current data type and does not represent an enumeration, struct, or instance of a class.
Ii. static methods in enumerations
Let's look at an example of an enumeration of static methods, with the following code:

[HTML]View Plaincopy print?
  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 func Interestby (amount:double)-> double {②
  8. Return InterestRate * Amount
  9. }
  10. }
  11. Calling a static method
  12. println (Account.interestby (10_000.00)) ③



The code above is to define the account enumeration, and the ① line code declares the static property interestrate. The ② line code defines a static method Interestby, which, like static computed properties, cannot access instance properties or instance methods. The ③ line code is called by the static method.
As can be seen from the example, there is no difference in the use of static methods for structs and enumerations.
Third, static methods in class
Let's look at an example of a class method, with the following code:

[HTML]View Plaincopy print?
  1. Class Account {
  2. var owner: String = "Tony"//account name
  3. Class func Interestby (amount:double)-> double {①
  4. Return 0.8886 * Amount
  5. }
  6. }
  7. Calling a static method
  8. println (Account.interestby (10_000.00)) ②



The code above defines the account class, and the ① code uses the keyword class to define a static method Interestby, which is similar to a static computed property, and it cannot access instance properties or instance methods. The ② line code is called by the static method.

Swift static method

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.