"IOS" swift classes and structures, storage properties, computed properties, functions and methods, satellite 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 : ( point 2,3 is not present in other languages )

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

2) can be initialized ( by way of construction )

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


Multi-function analogy structure :

1) can inherit

2) The runtime can check the type of the class object

3) Destructor Release Resources

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

Class person1{    var name = "Jack"}struct person2{    var age:int}//1.1 instantiate class with struct, default constructor let P1 = Person1 () let P2 = Person2 (age:10)  When using the default constructor, all of the properties inside must be initialized

Second, attributes ( storage Properties- - computed Properties- - class properties )

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

Class person3{    var name:string = "Xuneng"  //requires manual initialization of    var age:int = ten let    height = 175    lazy var p1:pers On1 = Person1 ()//delay, must be var}var p3 = Person3 () p3.name//   through point syntax to access p3.age = one   //set//Delay lazy Storage properties: initialization When using the change attribute, benefits is to avoid a space waste println (P3.P1)   //This call when P1 is initialized

2.2 Computed properties : do not store values directly , but instead use get/set . Can be used 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

Class person4{    var name:string = "Jack"    var jjlength:int = ten    var age:int{  //can only be var        get{            Return jjlength * 2        }        set (newage) {            jjlength = newage/2        }    }}var P4 = Person4 () P4.age = ten   //When set a GE is 10 o'clock and Jjlength becomes 5

2.2.1 Simple calculation of attribute notation

Default parameter NewValue, which represents the newly assigned value class person5{    //with newvalue    var jjlength:int = ten    var age:int{  //can only be var        get{            return jjlength * 2        }        set{            jjlength = Newvalue/2        }    }        //read-only computed property    var height:int {        get{            return jjlength * 4        }    }        var height2:int{  //read-only direct can omit get        return jjlength * 4< C20/>}}var P5 = Person5 () P5.height//p5.height = ten  //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

Class person6{    //with newvalue    var jjlength:int =/    /    var height2:int = 10{  //cannot be directly assigned, otherwise it is stored as attribute//        return jjlength * 4//    }        var age:int{        get{            return age   //dead loop.  Age calls the Get method        }        set{Age           = newvalue    //Dead loop.  The set method is called when the age value is set}}}    

the 2.3 class attribute ( or type attribute ) is 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

Class person7{    class var name:string{        return "Xuneng"    }}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 computing properties . Very useful for storage properties

Willset and Didset are not called when initializing a value

Willset, Didset and set, get cannot coexist

Class person8{    var name:string = "Xuneng" {        willset{            println ("New value: \ (newvalue)")    //newvalue new Value        }        didset{            println ("New value: \ (oldValue)")   //oldvalue represents the old value        }    }}var P8 = Person8 () P8.name = "Jack"    This sentence calls

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 . The name is the name of the parameter

Class person9{    func sum (num1:int, num2:int) {        println (num1 + num2)    }}var p9 = Person9 () p9.sum (num2:10 )  ///After the first parameter, it is all external parameters. The equivalent of adding #

The 3.2 class method . Modify with class

Class person10{    class func sum (Num1:int, num2:int) {        println (num1 + num2)    }}person10.sum (Ten, Num2:10)

3.3 Self vs . Super , same as OC

Class person11{    func sum (num1:int, num2:int) {        self.fun ()   //object method calls an object method, a method called class method    }        func Fun ( ) {        println ("Hello")    }}

Four, subscript (secondary script)

subscript, some are called subordinate scripts (subscript), new things.

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

Format: You can also set Set/get/*subscript (Index:int), int{    get{            }    set{            }}*/

struct person12{   //official lot with struct    let jjlength:int/      /struct here can not initialize        subscript (index:int)-int{        return jjlength * Index    }} var p12 = Person12 (jjlength:10) p12[3]     //is accessed directly like an array of accesses.  According to JJ length, index is 3 and the value is 30

Reference:

The Swift programming Language

Apple Dev Center


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



"IOS" swift classes and structures, storage properties, computed properties, functions and methods, satellite scripts, etc.

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.