Swift classes and structures, storage properties, computed properties, functions and methods, subordinate scripts, etc.

Source: Internet
Author: User
Tags set set

Write 12 person to review, but to complete the same code need to hit the keyboard is a lot less than OC, which is due to the Swift does not write a semicolon, and less oc in brackets.

I. CLASS and Structure

There's not much difference between the two in Swift.

There are many similarities between classes and structs : ( 2, 3 points are not present in other languages )

1) You can define properties /Methods /subscripts (structs can also define methods )

2) can be initialized (by means of construction )

3) can use extension (extension) and Protocol (protocol)

4) Global Properties/methods in classes and structs (enumerations ) : In classes with the class keyword , but in structs with static

Multi-function analogy structure :

1) can Inherit

2) The type of class object can be checked at runtime

3) destructor Release resources

4) Reference counting allows a class instance to have multiple references

[Java]View Plaincopy
  1. Class person1{
  2. var name = "Jack"
  3. class var height:int = 10//Error! A class cannot have a global storage property, only a computed property. See note below
  4. }
  5. struct person2{ //struct more like a class in Java
  6. var age:int
  7. static var Height:int = 10//can set global properties and need to be initialized.
  8. }
  9. 1.1 Instantiating classes and structs, default constructors
  10. Let P1 = Person1 ()
  11. Let P2 = Person2 (age: Ten) //when using the default constructor, all of the properties inside must be initialized

Second, attribute (store attribute --COMPUTE attribute --Class attribute )

2.1 Store Properties : is to store a constant or variable , similar to a member variable in Java

[Java]View Plaincopy
  1. Class person3{
  2. var name:string = "Xuneng" //Manual initialization required
  3. var age:int = Ten
  4. Let height = 175
  5. Lazy var p1:person1 = Person1 () //delay, must be var
  6. }
  7. var p3 = Person3 ()
  8. P3.name //access by DOT syntax
  9. P3.age = One //set
  10. Defer Lazy Store Properties: Initialize when using properties, the benefit of avoiding wasted space
  11. println (P3.P1) //This is called when P1 is initialized

2.2 Computed Properties : Do not store values directly , but instead use get/set . To manipulate changes in other property values

Get: Take , the process used to encapsulate a value

Set: Set , used to encapsulate the process of setting a value

[Java]View Plaincopy
  1. Class person4{
  2. var name:string = "Jack"
  3. var jjlength:int = Ten
  4. var age:int{ //can only be var
  5. get{
  6. return jjlength * 2
  7. }
  8. Set (NewAge) {
  9. Jjlength = newage/ 2
  10. }
  11. }
  12. }
  13. var P4 = Person4 ()
  14. P4.age = Ten //When set to age 10 o'clock, Jjlength becomes 5

2.2.1 Simple calculation of attribute notation

[Java]View Plaincopy
  1. Default parameter NewValue, which represents the newly assigned value
  2. Class person5{
  3. //With NewValue
  4. var jjlength:int = Ten
  5. var age:int{ //can only be var
  6. get{
  7. return jjlength * 2
  8. }
  9. set{
  10. Jjlength = newvalue/ 2
  11. }
  12. }
  13. //read-only computed properties
  14. var height:int{
  15. get{
  16. return jjlength * 4
  17. }
  18. }
  19. var height2:int{ //read-only direct can omit get
  20. return jjlength * 4
  21. }
  22. }
  23. var P5 = Person5 ()
  24. P5.height
  25. P5.height = 10//read-only cannot be assigned



2.2.2 Note the dead loop condition of the computed attribute

The value of the computed property is not fixed, so it cannot be decorated with a let and can only be used with var

Computed properties cannot be directly assigned to a value

[Java]View Plaincopy
  1. Class person6{
  2. //With NewValue
  3. var jjlength:int = Ten
  4. var height2:int = 10{//cannot be assigned directly, otherwise it is stored as a property.
  5. Return Jjlength * 4
  6. //    }
  7. var age:int{
  8. get{
  9. return age //Dead loop. Age calls the Get method
  10. }
  11. set{
  12. Age = NewValue //Dead loop. The set method is called when the age value is set
  13. }
  14. }
  15. }

Class 2.3 Attributes (or type attributes ) are decorated with the Class keyword . Class properties can only be computed properties .

A class property is a property similar to the static decoration in Java . A class has only one copy , and multiple instance objects are shared . Can be called directly with the class name

[Java]View Plaincopy
    1. Class person7{
    2. class var name:string{
    3. return "Xuneng"
    4. }
    5. }
    6. Person7.name

2.4 Property Monitor: 2 methods that can be used to monitor property changes willset, Didset

Computed properties because of the get and set methods , the monitor is actually not very useful for calculating properties . Useful for storing properties

willset and Didset are not called when initializing a value

Willset, Didset and set, get cannot coexist

[Java]View Plaincopy
  1. Class person8{
  2. var name:string = "Xuneng" {
  3. willset{
  4. println ("new value: \ (newvalue)") //newvalue new value
  5. }
  6. didset{
  7. println ("new value is: \ (oldValue)") //oldvalue represents the old value
  8. }
  9. }
  10. }
  11. var P8 = Person8 ()
  12. P8.name = "Jack" //This sentence is called


Three, function and method

3.1 Object Methods

The difference between a method in a class and a function : After the first argument of the method , it is all an external parameter . Name is the parameter name

[Java]View Plaincopy
    1. Class person9{
    2. Func sum (num1:int, num2:int) {
    3. println (NUM1 + num2)
    4. }
    5. }
    6. var p9 = Person9 ()
    7. P9.sum (num2: Ten) //After the first parameter, it is all external parameters. The equivalent of adding #

The 3.2 class method. Modify with class

[Java]View Plaincopy
    1. Class person10{
    2. class func sum (Num1:int, num2:int) {
    3. println (NUM1 + num2)
    4. }
    5. }
    6. Person10.sum (Ten, num2: ten)

3.3 Self vs. super, same as OC

[Java]View Plaincopy
    1. Class person11{
    2. Func sum (num1:int, num2:int) {
    3. Self.fun () //Object method Call object method, class method call class method
    4. }
    5. Func Fun () {
    6. println ("Hello")
    7. }
    8. }


Four, subscript (secondary script)

Subscript, Some are called subordinate scripts (subscript), new things.

Can be defined in the class /struct /enumeration for quick access.

[Java]View Plaincopy
    1. Format: You can also set Set/get
    2. /*
    3. Subscript (index:int), int{
    4. get{
    5. }
    6. set{
    7. }
    8. }
    9. */

[Java]View Plaincopy
    1. struct person12{   // The official structure of many   
    2.     let jjLength:Int       //struct here can not initialize   
    3.        
    4.     subscript (index:int)  -> int{  
    5. Li class= "alt" >         return jjlength  * index  
    6.     }  
    7. }   
    8.   
    9. var p12 = person12 (Jjlength: 10"   
    10. p12[3]      //accesses the .   directly like an array, according to the JJ length, index is 3, and the value is 30  

See Example code: http://github.com/xn4545945/SwiftLearning

Reference:

The Swift programming Language

Apple Dev Center

Reprint Please specify source: http://blog.csdn.net/xn4545945

Swift classes and structures, storage properties, computed properties, functions and methods, subordinate scripts, etc.

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.