selector在Objective-C裡面,通常被拿來當作callback function使用。以下是網路上看來的,先記起來,以免日後要找時忘記。
SEL is a type that represents a selector in Objective-C. The @selector() keyword returns a SEL that you describe. It’s not a function pointer and you can’t pass it any objects or references of any kind. For each variable in the selector (method), you have to represent that in the call to @selector. For example:
1.
-(
void
)methodWithNoArguments;
2.
SEL noArgumentSelector = @selector(methodWithNoArguments);
3.
4.
-(
void
)methodWithOneArgument:(id)argument;
5.
SEL oneArgumentSelector = @selector(methodWithOneArgument:);
6.
7.
-(
void
)methodWIthTwoArguments:(id)argumentOne and:(id)argumentTwo;
8.
SEL twoArgumentSelector = @selector(methodWithTwoArguments:and:);
Selectors are generally passed to delegate methods and to callbacks to specify which method should be called on a specific object during a callback. For instance, when you create a timer, the callback method is specifically defined as:
1.
-(
void
)someMethod:(NSTimer*)timer;
So when you schedule the timer you would use @selector to specify which method on your object will actually be responsible for the callback:
01.
@implementation MyObject
02.
03.
-(
void
)myTimerCallback:(NSTimer*)timer
04.
{
05.
06.
if
( timerShouldEnd ) {
07.
[timer invalidate];
08.
}
09.
}
10.
11.
@end
12.
13.
14.
15.
int
main(
int
argc,
const
char
**argv)
16.
{
17.
18.
MyObject* obj = [[MyObject alloc] init];
19.
SEL mySelector = @selector(myTimerCallback:);
20.
[NSTimer scheduleTimerWithTimeInterval:30.0 target:obj selector:mySelector userInfo:nil repeats:YES];
21.
22.
return
0;
23.
}
In this case you are specifying that the object obj be messaged with myTimerCallback every 30 seconds.
假如一個function在該做的事完成後要執行selector callback,可以類似下面這樣寫
1.
-(
void
) someMethod:(id)handler selector:(SEL)selector {
2.
3.
4.
5.
if
( handler != nil && selector != nil && [handler respondsToSelector:selector] ) {
6.
[handler performSelector:selector];
7.
}
8.
}
Reference:
http://stackoverflow.com/questions/297680/how-do-sel-and-selector-work-in-iphone-sdk
分享到:
相关推荐
Objective-C语言中方法的传递有二种:①Selector ② Blocks,本文主要说一下Selector,关于Blocks会在后续总结一下。 消息传递模型(Message Passing)是Objective-C语言的核心机制。在Objective-C中,没有方法调用...
第 1 章,也就是本章 第 2 章,从 Hello,World!...第 5 章,Class 类型,选择器 Selector 以及函数指针 第 6 章,NSObject 的奥秘 第 7 章,对象的初始化以及实例变量的作用域 第 8 章,类方法以及私有方法
objective-c小技巧 1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat...
Software engineering,programming language,operating system,iOS,OS,iPhone,iPad objective c,cocoa touch,SDK,object oriented design,Apple,Macintosh,tools,language,runtime,Xcode,objective-...
Objective-C iOS原生开发语言Objective-C。从0到1开始的demos。 5.8 对storyboard深入了解: 常见的报错: reason: '-[ViewController redBtn]: unrecognized selector sent to instance 0x7ff62ff05750' 多余的连线...
围绕Objective-C运行时的轻巧包装。lumos到底是什么? 如前所述,lumos是对objective-c运行时函数的轻量包装,可以更轻松地访问运行时。 它使Swift中的翻页和挂接等操作非常简单。 例如,假设您希望每当调用...
从 .net 框架切换到 Objective-C 时,我们错过的最重要的事情之一是中 Linq to Objects 扩展方法的集合。 当我们在上工作时,我们发现我们正在处理很多集合,就像我们曾经在 .net 中所做的那样。 在 Objective-C ...
Objective-C 的简单存根 NSObject <SBStub> *stub = [Stubbilino stubObject: [[ NSObject alloc ] init ]]; [stub stubMethod: @selector ( description ) withBlock: ^{ return @" Stubbilino is awesome! " ; }...
使用Objective-C的注释实现 0x00摘要 注释是一种语法元数据的形式,可以添加到Objective-C源代码中,例如java注释。 0x01它能做什么? @when用于事件调度。 @When(AppLaunched,ViewController, @selector ( ...
这个插件只支持 Objective-C。 ##特征 复制类名 复制表达式 复制为选择器或@selector()或NSSelectorFromString() 复制为objc_msgSend()片段 复制为符号断点表达式 复制为宏片段 支持属性 getter/setter 按住 ...
JNSwapIMP 是一个源代码包,它提供了一个快速简单的接口来侵入 Objective-C 运行时。 JNSwapIMP 允许您从继承自 NSObject 的任何对象中交换任何方法的实现,从而非常容易重新设计特定方法的功能或侵入密封类并重新...
一个满足您日常HTML需求的Objective-C框架。 快速概述 HTMLKit是符合的框架,用于解析和序列化iOS和OSXHTML文档和文档片段。 HTMLKit以与现代Web浏览器相同的方式解析实际HTML。 HTMLKit提供了丰富的DOM实现,用于...
@property (nonatomic) CGFloat maxProgress UI_APPEARANCE_SELECTOR; // Max value of progress @property (nonatomic) CGFloat minProgress UI_APPEARANCE_SELECTOR; // Min value of progress @property ...
BlockTracker BlockTracker可以跟踪方法的块参数。 它基于 。...__unused BTTracker *tracker = [ self bt_trackBlockArgOfSelector: @selector ( performBlock: ) callback: ^(BHInvocation * _Nonnull
前言 正在运行的 APP 突然 Crash,是一件令人不爽...Unrecognized Selector Crash KVO Crash Container Crash NSNotification Crash NSNull Crash NSTimer Crash 野指针 Crash 1 Unrecoginzed Selector Crash 出现原因
In this chapter, you'll use DTrace to hook objc_msgSend's entry probe and pull out the class name along with the Objective-C selector for that class. By the end of this chapter, you'll have LLDB ...
PLPatchMaster PLPatchMaster 提供了一个易于使用的基于块的 API,使用了提供的块蹦床库... [UIWindow pl_patchInstanceSelector: @selector(sendEvent:) withReplacementBlock: ^(PLPatchIMP *patch, UIEvent *event)
photo-selector-to-animation 照片选择器转场动画
在Objective-C中调用一个方法,其实是向一个对象发送消息,查找消息的唯一依据是selector的名字。利用Objective-C的动态特性,可以实现在运行时偷换selector对应的方法实现,达到给方法挂钩的目的。 每个类都有一个...