Objective C SEl 和@selector是怎么工作的||How do SEL and @selector work in iphone sdk?
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?的更多相关文章
- 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 ...
- @selector和SEL
遇到selector发现不是很明白,网上搜到的零零星星的介绍也不成体系,索性自己翻译一下,加深一下印象.原文来自官方API文档下的Selectors. Selectors 在OC中,selector有 ...
- NIO组件Selector工作机制详解(上)
转自:http://blog.csdn.net/haoel/article/details/2224055 一. 前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但 ...
- objective-c中的@selector()和 c /c++的函数指针
先看tomcat里用到的代码: //然后开始动画 //把图片放到animationImages,接受数组参数 self.tom.animationImages = arrayImage; //设置时间 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- Objective C运行时(runtime)技术的几个要点总结
前言: Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...
- Java NIO教程 Selector
这次我们开讲非阻塞I/O中的Selector,它需要配合非阻塞的TCP和UDP来使用.首先我们先简单讲一下TCP和UDP的非阻塞通道. 非阻塞I/O通道 在上代码前我们先讲解一些最基本的知识.TCP和 ...
- Object C学习笔记18-SEL,@ selector,Class,@class
本章是对上一章<<Object C学习笔记17-动态判断和选择器>>的一点补充,所以比较简单点. 一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体 ...
- ***iOS开发中@selector的理解与应用
@selector 是什么? 1一种类型 SEL2代表你要发送的消息(方法), 跟字符串有点像, 也可以互转.: NSSelectorFromString() / NSSelectorFromStri ...
随机推荐
- CSS基础知识点(二)——选择器
在这一篇中,主要总结一下CSS中的选择器. 参考:http://www.cnblogs.com/webblog/archive/2009/07/07/1518274.html CSS中的选择器主要包括 ...
- spark 分析sql内容再插入到sql表中
package cn.spark.study.core.mycode_dataFrame; import java.sql.DriverManager;import java.util.ArrayLi ...
- window7资源管理器一直重启(百度知道找到可用)
今天我的机器也出现这种问题:我的解决方式是,在开机时选择系统修复选项中的进入命令行方式(尝试过用安全模式,文件被占用,现象一样),然后cd C:\Users\Administrator\AppData ...
- Android—进度条
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- 一些基本的C/C++数据类型
size_t size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof ...
- A Flock Of Tasty Sources On How To Start Learning High Scalability
This is a guest repost by Leandro Moreira. When we usually are interested about scalability we look ...
- 如何处理ABBYY中出现错误代码142和55的问题
在使用ABBYY FineReader 12OCR文字识别软件创建PDF文件时,有时会出现以下错误提示:内部程序错误..\Src\SpecialFontFactory.cpp,142和内部程序错误.. ...
- apache mesos 安装
安装方式: Mesosphere yum 操作系统:centos7 1. 安装 rpm -Uvh http://repos.mesosphere.io/el/7/noarch/RPMS/mesos ...
- winrar 5.21去广告
http://www.rarlab.com/ 把下面的数据复制到“记事本”中,用文件名“rarreg.key搜索”命名该文件,保存到winrar安装文件夹即完成注册 RAR registration ...
- @media_screen
html,body{ margin:0; padding:0; }body{ background:#0f0;} /* @media screen and (min-width: 800px)and( ...