Method references in the Swift programming language (based on version 2.2)

Source: Internet
Author: User

Because Apple's official "The Swift Programming Guide" does not introduce much of the method references in the SWIFT programming language, the method references in Swift are described in more depth and detail here.

Swift is different from Objective-c, because the Objective-c method belongs to "message", so it is directly signed with a selector message to represent a definite message as a method reference. Swift's approach is more like the method in C + +, Java, which means it is more static than objective-c, so it is not so flexible.

Also, in the swift programming language, method references differ from those in C + +, and similar to Java, are bound to the current object. If a method refers to a method of a class that is pointed to, and the method is not overloaded, then the object name can be used directly . method Name is the way to represent it. If the method is overloaded, then a complete description of the formal parameter is required, which is similar to the message signature in Objective-c, which requires the label name (external name) of the formal parameter to be added. However, because Swift is a strongly typed language, you can also specify the type of the method reference directly to determine which overloaded method to point to.

classViewcontroller:nsviewcontroller {Overridefunc viewdidload () {super.viewdidload ()//because the MyMethod method is overloaded,//so here Funcref explicitly specify a type to indicate a MyMethod method that points to no parameters         LetFuncref: ()Void=Self  . MyMethod funcref ()//here, the method signature MyMethod (a:) is used to indicate the point to MyMethod (a a:int) method        varMethodRef =Self  . MyMethod (A:) MethodRef (A: -)                //Here you use the method signature MyMethod (_:) to indicate the point to MyMethod (_ A:int) methodMethodRef =Self  . MyMethod (_:) MethodRef (A: $)} func MyMethod () {print ("My method!")} func MyMethod (a a:int) {print ("Method2 value = \ (a)")} func MyMethod (_ A:int) {print ("Method3 value = \ (a)")    }        Override varRepresentedobject:anyobject?{didset {//Update The view, if already loaded.        }    }}

In the above code, we cannot use Self.mymethod () to represent a method reference without parameters, or to use MyMethod (_), because both forms already represent method invocations, not method signatures. In a method signature, you must include at least one colon in the formal parameter list.

Of course, in Swift2.2, Self.mymethod in the case of overloaded methods can not directly represent the method without any formal parameters, it is very regrettable ~ if the FUNCREF does not indicate the function type, then the compiler will report "Ambiguous method name" error. In fact, in Swift, if a method contains a parameter, there is bound to be a colon, regardless of whether the parameter has an external name, so hopefully this can be improved in the Swift 3.x version!

Method references in the Swift programming language (based on version 2.2)

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.