@selector () in Swift

Source: Internet
Author: User

1, in the OBJC

In OBJC, you can use @selector to obtain the SEL type of a method or function, which is the method passed in the method as a parameter.

In OBJC, in addition to @selector, nsselectorfromstring can obtain the SEL type, and the corresponding nsstringfromselector function is to convert the SEL type to a string.

2, in Swift

In Swift, the selector type corresponds to the SEL type. In Swift, the string name (full name) of a method or function is passed directly to the place where the selector type is required, because selector has such a specified constructor:

Init (_ str: String)


And make this automatic conversion feasible. Because there is an underscore (_) before the construction of the parameter str, you do not have to pass STR when you use it.


In addition, in Swift,nsselectorfromstring and nsstringfromselector are still available, as shown in the code below:


func F1 () {

println("F1")

}

class C1 {

var var1 = "LY"

}


Nsselectorfromstring

Let a = (Selector("F1"),nsselectorfromstring("F1" ))

Selector ("F1") = = nsselectorfromstring("F1")//true

Nsstringfromselector

Let B = (String(_sel: a. 0),nsstringfromselector(a. 0))

String (_sel: a. 0 ) = = Nsstringfromselector(a. 0 )//true


Let d = Nsstringfromclass(C1)

Let C = Nsclassfromstring(D)


The global functions available in OBJC are still available, and Swift brings us more convenience.


Summarize the relevant methods:

    1. @selector (func), used in OBJC, returns the SEL
    2. Selector ("func name") is used in Swift to return the Selector type, but this usage is not necessary because Swift has an attribute of automatic type inference, where a string is passed in where the type argument is required, string is automatically converted to the selector type
    3. Nsselectorfromstring, both available, return SEL or selector
    4. Nsstringfromselector, both available, return NSString or string
    5. Nsstringfromclass, both available, return NSString or string
    6. Nsclassfromstring, both are available, return class in OBJC, and return Anyclass in Swift.


Learn Swift technology, join the Swift book Friends QQ Group: 259152129, learn to discuss with the author!

@selector () in Swift

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.