Created
December 16, 2016 10:08
-
-
Save josephchang10/8ed6f5d0e0aa5c578a131e5f96e58552 to your computer and use it in GitHub Desktop.
Selector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void) callMe { | |
//... | |
} | |
-(void) callMeWithParam:(id)obj { | |
//... | |
} | |
SEL someMethod = @selector(callMe); | |
SEL anotherMethod = @selector(callMeWithParam:); | |
// 或者也可以使用 NSSelectorFromString | |
// SEL someMethod = NSSelectorFromString(@"callMe"); | |
// SEL anotherMethod = NSSelectorFromString(@"callMeWithParam:"); |
一般为了方便,很多人会选择使用 @selector
,但是如果要追求灵活的话,可能会更愿意使用NSSelectorFromString
的版本——因为我们可以在运行时动态生成字符串,从而通过方法的名字来调用到对应的方法。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@selector
是Objective-C时代的一个关键字,它可以将一个方法转换并赋给一个SEL
类型,它的表现很类似一个动态的函数指针。在 Objective-C 时 selector 非常常用,从设定 target-action,到自举询问是否响应某个方法,再到指定接受通知时需要调用的方法等等,都是由 selector 来负责的。在 Objective-C 里生成一个 selector 的方法一般是这个样子的。