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:

-(void)methodWithNoArguments;
SEL noArgumentSelector =@selector(methodWithNoArguments);
-(void)methodWithOneArgument:(id)argument;
SEL oneArgumentSelector =@selector(methodWithOneArgument:);// notice the colon here注意引号
-(void)methodWIthTwoArguments:(id)argumentOne and:(id)argumentTwo;
SEL twoArgumentSelector =@selector(methodWithTwoArguments:and:);// notice the argument names are omitted

Selectors are generally passed to delegate methods and to callbacks to specify which method should be called on a specific object during a callback.

一个计时器的例子:@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 ;
}
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeat
这个是NSTimer里的方法;
这句代码的意思就是每隔30s会调用selector 代表的方法(回调方法)myTimerCallback;
你可以理解 @selector()就是取类方法的编号,他的行为基本可以等同C语言的中函数指针,但不是指针,只不过C语言中,可以把函数名直接赋给一个函数指针,而Objective-C的类不能直接应用函数指针,这样只能做一个@selector语法来取.

Objective C SEl 和@selector是怎么工作的||How do SEL and @selector work in iphone sdk?的更多相关文章

  1. Shape + Selector: Make a Shape as one item of the Selector

    Generally, I use a selector to select pictures or colors to render the normal and the pressed backgr ...

  2. @selector和SEL

    遇到selector发现不是很明白,网上搜到的零零星星的介绍也不成体系,索性自己翻译一下,加深一下印象.原文来自官方API文档下的Selectors. Selectors 在OC中,selector有 ...

  3. NIO组件Selector工作机制详解(上)

    转自:http://blog.csdn.net/haoel/article/details/2224055 一.  前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但 ...

  4. objective-c中的@selector()和 c /c++的函数指针

    先看tomcat里用到的代码: //然后开始动画 //把图片放到animationImages,接受数组参数 self.tom.animationImages = arrayImage; //设置时间 ...

  5. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  6. Objective C运行时(runtime)技术的几个要点总结

    前言:          Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...

  7. Java NIO教程 Selector

    这次我们开讲非阻塞I/O中的Selector,它需要配合非阻塞的TCP和UDP来使用.首先我们先简单讲一下TCP和UDP的非阻塞通道. 非阻塞I/O通道 在上代码前我们先讲解一些最基本的知识.TCP和 ...

  8. Object C学习笔记18-SEL,@ selector,Class,@class

    本章是对上一章<<Object C学习笔记17-动态判断和选择器>>的一点补充,所以比较简单点. 一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体 ...

  9. ***iOS开发中@selector的理解与应用

    @selector 是什么? 1一种类型 SEL2代表你要发送的消息(方法), 跟字符串有点像, 也可以互转.: NSSelectorFromString() / NSSelectorFromStri ...

随机推荐

  1. ES6-函数扩展

  2. OC小结

    #import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * a ...

  3. eBay_Relist(退换刊登费)

    如果物品首次刊登结束时没有人中标,或是买家并没有付款完成交易,那么卖家以通过重新刊登的方法来再次销售.如果该物品在第二次刊登时成功售出,且满足eBay的重新刊登退费条件,那么eBay便会退还重新刊登的 ...

  4. linux笔记_文件搜索命令

    一.locate命令 locate命令属于mlocate包,如果执行locate filename提示命令未找到执行安装mlocate包 # yum -y install mlocate 安装后执行l ...

  5. LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!

    超时代码 class Solution { public: vector<int> findSubstring(string s, vector<string>& wo ...

  6. java编程acm基础

    java还是不错的昂! import java.util.*; import java.io.*; public class text{ static int a=100; public static ...

  7. 《苹果开发之Cocoa编程》挑战1 创建委托 练习

    <苹果开发之Cocoa编程>第4版 P87 新建一个单窗口应用程序,设置某对象为窗口的委托,当用户调整窗口尺寸时,确保窗口高度为宽度的2倍. 需要实现的委托方法为:-(NSSize)win ...

  8. kuangbin_ShortPath E (POJ 1860)

    第一次做判环 然后RE了五次 死在了奇怪的点 memset(vis, 0, sizeof dis); memset(dis, 0, sizeof vis); 什么鬼?? 什么鬼?? 其实代码本身还是不 ...

  9. 通过js对表单对象的便捷获取

    <form name="a"> <input name="s" value="33"/> </form> ...

  10. mysql学习之-密码管理(默认密码,修改密码,解决忘记密码)

    1. mysql安装后默认没有密码,初始化安装后默认密码登录,需要马上修改root密码.[root@mysql ~]# cat /root/.mysql_secret    --查看root账号密码# ...