About @selector
来源:互联网 发布:淘宝买真货退假货 编辑:程序博客网 时间:2024/06/08 06:50
A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled. A selector by itself doesn’t do anything. It simply identifies a method. The only thing that makes the selector method name different from a plain string is that the compiler makes sure that selectors are unique. What makes a selector useful is that (in conjunction with the runtime) it acts like a dynamic function pointer that, for a given name, automatically points to the implementation of a method appropriate for whichever class it’s used with. Suppose you had a selector for the method run, and classes Dog, Athlete, andComputerSimulation (each of which implemented a method run). The selector could be used with an instance of each of the classes to invoke its run method—even though the implementation might be different for each.
Getting a Selector
Compiled selectors are of type SEL. There are two common ways to get a selector:
At compile time, you use the compiler directive @selector.
At runtime, you use the NSSelectorFromString function, where the string is the name of the method:
You use a selector created from a string when you want your code to send a message whose name you may not know until runtime.
Using a Selector
You can invoke a method using a selector with performSelector: and other similar methods.
(You use this technique in special situations, such as when you implement an object that uses the target-action design pattern. Normally, you simply invoke the method directly.)
如何检测一个类中有没有某个selector
-respondsToSelector:+instancesRespondToSelector:例子:
@interface Parent : - (void)onUpdate;@end@interface Child : Parent @end则
Child * childObj = [[Child alloc] init];BOOL hasSelector = [childObj respondsToSelector : @selector(onUpdate)];CCLOG(@"child has selector = %d", hasSelector); BOOL hasInstanceSelector = [Child instancesRespondToSelector : @selector(onUpdate)];CCLOG(@"child has instance selector = %d", hasInstanceSelector); BOOL hasParentInstanceSelector = [Parent instancesRespondToSelector : @selector(onUpdate)];CCLOG(@"parent has parent instance selector = %d", hasParentInstanceSelector);结果为child has selector = 1child has instance selector = 1parent has parent instance selector = 1
- About @selector
- selector
- selector
- @selector
- selector
- @selector
- Selector
- @selector
- @selector
- Selector
- selector
- @selector
- selector
- selector
- Selector
- @selector
- SELECTOR
- selector
- Extjs4.1:gridPanel不显示数据问题的解决
- Linux alsa 声卡驱动 安装 卸载 设置默认声卡
- ios 平台编译器支持一览表
- Oracle Agile 有用户无法收到PLM系统发出的相关邮件通知
- epoll_create, epoll_ctl和epoll_wait
- About @selector
- 前后台系统的优缺点
- mac下eclipse支持GBK
- C#时间格式大全
- Linux设置系统时间
- HttpModule和URLReWriter实现伪静态
- Android中NFC功能流程图解析及代码演示
- UITextField输入限制
- struts2 result type分析