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:
- @selector (func), used in OBJC, returns the SEL
- 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
- Nsselectorfromstring, both available, return SEL or selector
- Nsstringfromselector, both available, return NSString or string
- Nsstringfromclass, both available, return NSString or string
- 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