Swift: Instance method and Type method

Source: Internet
Author: User

We should be familiar with the concept of "instance method and type method", which is very common in objective-c. For example:

1. Instance method (minus sign start)

-(instancetype) init;
when calling, you must instantiate an object (Alloc) before calling the Init method.

2. Type method (the plus sign begins)

+ (void) Animatewithduration: (nstimeinterval) duration animations: (void (^) (void)) animations
when calling, you can use the UIView type directly.

The wording in Swift has changed, and it is marked with the class and static keywords. And not just in classes, enumerations and structs (enums, structs) have corresponding methods.

In the context of class type, we use class, others use static.

first, in the structure ( Note: In the code, about the storage properties and the calculation of the properties of knowledge, I will be in the next section of the detailed introduction. )

struct Point {let    x:double let    y:double        //store property    static Let zero = Point (x:0, y:0)//        Compute Property    STA tic var ones: [Point] {        return [point (            x:1, y:1), point            (x:-1, Y:1), point (            x:1, y:-1), point            (x:  -1, Y:-1)        ]    }    /Type method    static func Add (P1:point, P2:point), point {        return point (x:p1.x +  p2.x, Y:p1.y + p2.y)    }}let p = Point (X:3, Y:4) Point.zeroPoint.onesPoint.add (Point (X:1, Y:2), P2:point (X:2, y: 1))

As you can see, in a struct, we can add static to the property or method before it is modified, and the corresponding property or method can be called directly by the type name " point ".


Second, acting on the class

Class Square {    //Type attribute, with class keyword    class var pi:double{        return 3.14    }}

As you can see, in a class, we can add class in front of its properties or methods to modify it, and call it directly square.pi. The code is class-decorated with "computed properties". The computed attribute here is shorthand, and we can write it in the following way:

Class Square {    class var pi:double{        get {return 3.14}}    }


However, it is important to note that storage properties cannot be decorated with class.

Class Student {    class var phone:string?}

error after compiling: "class variables not yet supported"

And with static it can be.

Class Student {    static var phone:string?}


Third, acting on the agreement

Protocol MyProtocol {    static func foo (), string}struct mystruct:myprotocol {    static func foo () String {        return "mystruct"    }}class MyClass {    class func foo () String {        return "MyClass"}    }

As you can see, the property or method defined in protocol is preceded by the static modifier, but the problem is that the implementation of the Protocol can be either a struct or an enumeration, or it might be a class. What modifiers should be used in the implementation body? By observing the above notation, you can see:

The type modifier is static for the method defined in protocol or for the computed property, the modifier uses static in the implemented struct or enumeration, and the modifier uses class in the implemented class.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift: Instance method and Type method

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.