SEL是一种表示Objective-C中选择器的类型。 @selector()关键字返回您描述的SEL。它不是一个函数指针,你不能传递任何对象或任何类型的引用。对于选择器(方法)中的每个变量,必须在对@selector的调用中表示该变量。例如:
-(void)methodWithNoParameters;
SEL noParameterSelector = @selector(methodWithNoParameters);
-(void)methodWithOneParameter:(id)parameter;
SEL oneParameterSelector = @selector(methodWithOneParameter:); // notice the colon here
-(void)methodWIthTwoParameters:(id)parameterOne and:(id)parameterTwo;
SEL twoParameterSelector = @selector(methodWithTwoParameters:and:); // notice the parameter names are omitted
选择器通常通过委派方法和回调来指定该方法应的特定对象上的回调过程中被调用。例如,当您创建一个计时器,回调方法具体定义为:
-(void)someMethod:(NSTimer*)timer;
所以,当你计划你可以使用@selector来指定哪些方法您的对象实际上将负责回调的计时器:
@implementation MyObject
-(void)myTimerCallback:(NSTimer*)timer
{
// do some computations
if(timerShouldEnd) {
[timer invalidate];
}
}
@end
// ...
int main(int argc, const char **argv)
{
// do setup stuff
MyObject* obj = [[MyObject alloc] init];
SEL mySelector = @selector(myTimerCallback:);
[NSTimer scheduledTimerWithTimeInterval:30.0 target:obj selector:mySelector userInfo:nil repeats:YES];
// do some tear-down
return 0;
}
在这种情况下,您指定每隔30秒用myTimerCallback向对象obj发送消息。
@Jim Puls--这实际上是一个Objective-C问题......对于可可或Cocoa-touch而言,它并不是特定于iPhone-sdk。此外,我们正在与objectivec标签这些天:)目标c :) – 2008-11-18 02:57:28