[Objective-C] selector 简介

参考:

http://www.jb51.net/article/32049.htm

http://www.jb51.net/article/32049.htm

http://rypress.com/tutorials/objective-c/methods


简而言之,你可以理解 @selector()就是取类方法的编号,他的行为基本可以等同C语言的中函数指针,只不过C语言中,可以把函数名直接赋给一个函数指针,而Objective-C的类不能直接应用函数指针,这样只能做一个@selector语法来取.
   
它的结果是一个SEL类型。这个类型本质是类方法的编号(函数地址)?因此我们有如下代码。

一、取得selector值.

C函数指针

  1. int add(int val)  
    {  
        return val+1;  
    }

    int (* c_func)(int val); //定义一个函数指针变量  
    _func = add ;  //把函数addr地址直接赋给c_func 

    Objective-C的选择器,
  1. @interface foo  
    -(int)add:int val;  
    @end  
    
    SEL class_func ; //定义一个类方法指针  
    class_func = @selector(add:int); 

注意:

  1. @selector是查找当前类的方法,而[object @selector(方法名:方法参数..) ] ;是取object对应类的相应方法.
  2. 查找类方法时,除了方法名,方法参数也查询条件之一.
  3. 可以用字符串来找方法 SEL 变量名 = NSSelectorFromString(方法名字的字符串);
  4. 可以运行中用SEL变量反向查出方法名字字符串:NSString *变量名 = NSStringFromSelector(SEL参数); 


二、执行selector值.

取得相应值后,怎么处理SEL值呢,这一点仍然与函数指针一样,就是执行它
   
函数指针执行,(以下有几种等效形式)

  1. *c_func(10);  
    c_func(10); 

SEL变量的执行.用performSelecor方法来执行. 

  1. [对象 performSelector:SEL变量 withObject:参数1 withObject:参数2]

三.selector的应用场合

selector本质是跟C的回调函数一样。主要用于两个对象之间进行松耦合的通讯.这种方法很多开发环境用到。比如GTK,Delphi。基本上整个Cocoa库之间对象,控制之间通讯都是在这个基础构建的


在Objective C中,selector有两个含义:

  1. 当在源代码中发消息给对象的时候,selector指的是方法名
  2. 当源代码被编译后,指的是替代方法名的唯一标识符(identifier)

编译后的selector的类型是SEL, 所有具有相同方法名的方法有相同的selector
你可以用selector来调用一个object的方法,这个为Cocoa中target-action的设计模式提供了基础


method and selector
从效率上说,编译后的代码中,ASCII的名字不会被用做方法的selector,编译器会把方法名写到一张表格里,用一个唯一的标识符(identifier)与它对应,并且在运行时用这个标识符来表示方法。标识符是唯一的,selector也是唯一的,没有两个相同的标识符或者selector。


SEL and @selector
为了区别于其他的数据类型,编译后的selector的类型为SEL。 指令@selector可以引用编译后的selector,这个要比使用方法名要有效率。


Methods and selectors
编译后的方法指示方法名,而不是方法的实现。这是多态和动态绑定的基础。这能让你发送相同的消息给不同的类的接收器,如果类方法名和对象方法名一样,
那么他们的selector标识符也是一样的,但是这不会冲突,首先selecotr标示符不是方法的实现,只代表方法名,其次类方法名和对象方法名属于不同的域。


Method Return and Parameter Types

 消息程序只能通过selector来访问方法的实现,所以对待所有具有相同selector的方法都是一样的。消息程序通过selector来获取返回类型和参数的类型
因此动态绑定需要所有具有相同名字的方法的实现有相同的return类型和相同的参数类型。


Varying the Message at Runtime

performSelector: 
performSelector:withObject:
performSelector:withObject:withObject: 
以上3个定义在NSObject protocol里的方法,把SEL标识符作为他们的初始参数。3个方法都直接映射到消息函数,举个例子,

[friend performSelector:@selector(gossipAbout:)withObject:aNeighbor];
相当于
[friend gossipAbout:aNeighbor];

有了这些方法,消息和接收对象在运行时是可变的。变量名可以用在消息表达式里,例:
id helper = getTheReceiver();
SEL request = getTheSelector();
[helper performSelector:request];


The Target-Action Design Pattern

在处理用户界面控制时,Appkit对于可变消息和可变接收对象运用的很好。例:
[myButtonCell setAction:@selector(reapTheWind:)];
[myButtonCell setTarget:anObject];

几个方法:

  1. SEL val = @selector(method);
  2. SEL val = NSSelectorFromString(methodString)
  3. NSString *methodString = NSStringFromSelector(sel_val)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Objective-C语言中方法的传递有二种:①Selector ② Blocks,本文主要说一下Selector,关于Blocks会在后续总结一下。

消息传递模型(Message Passing)是Objective-C语言的核心机制。在Objective-C中,没有方法调用这种说法,只有消息传递

在C++或Java中调用某个类的方法,在Objective-C中是给该类发送一个消息。在C++或Java里,类与类的行为方法之间的关系非常紧密,一个方法必定属于一个类,且于编译时就已经绑定在一起,所以你不可能调用一个类里没有的方法。而在Objective-C中就比较简单了,类和消息之间是松耦合的,方法调用只是向某个类发送一个消息,该类可以在运行时再确定怎么处理接受到的消息。也就是说,一个类不保证一定会响应接收到的消息,如果收到了一个无法处理的消息,那么程序既不会出错也不或宕掉,它仅仅是什么都不做,并返回一个nil【笔者添加:在编译期是不出错的,符合语义上的理解,但是runtime运行时的话,会崩溃】。

很显然,既然编译器不定位方法,那么只有运行期定位方法了,Objective-C又是怎么去运行期定位方位的呢?

id objc_msgSend(id receiver, SEL selector, ...)【包含二个必要参数:receiver(接受者对象)、selector(方法选择器)和一个未知参数(selector的参数列表)】

Objective-C就是通过上述方法来查找调用方法的。比如[itNoob cry]就被转换成objc_msgSend(itNoob,cry),这里receiver就是itNoob对象,selector就是cry选择器,当然如果cry拥有参数的话,会同样被转换,如[itNoob cry:@"呜呜" AndSmile:@"嘻嘻"]会被转换成objc_msgSend(itNoob,cry:AndSmile:,@"呜呜",@"嘻嘻"),类似如objc_msgSend(id receiver, SEL selector, parm1,parm2,...)。

objc_msgSend的动态绑定过程

  1. 根据receiver对象去查找selector方法的具体实现位置,调用查找到的实现,传递参数,将方法实现的返回值作为自己的返回值。

objc_msgSend查找方法的具体实现位置如下:

编译器构建每个类的时候,每个类必须包含二个必要的元素:

指向父类的指针一个调度表(dispatch table),调度表将类的selector与方法的实际内存地址关联起来。

我们知道每个对象都有一个isa指针,指向所属类,通过这个isa指针可以找到对象的所属类和所属的父类...

查找过程如下


当想一个对象发送消息的时候,先根据isa找到所属的类,然后去查找该类的dispatch table,如果没有找到,就去其父类中查找...如果找到了,就根据调度表中的内存地址调用该实现,如果最后一直没有找到返回nil


~~~~~~~~~~~~ 下面是来自Ry的一段小例子程序 ~~~~~~~~~~~

Selectors

Selectors are Objective-C’s internal representation of a method name. They let you treat a method as an independent entity, enabling you to separate an action from the object that needs to perform it. This is the basis of the target-action design pattern, which is introduced in theInterface Builder chapter of Ry’s Cocoa Tutorial. It’s also an integral part of Objective-C’s dynamic typing system.

There are two ways to get the selector for a method name. The@selector() directive lets you convert a source-code method name to a selector, and the NSSelectorFromString() function lets you convert a string to a selector (the latter is not as efficient). Both of these return a special data type for selectors called SEL. You can use SEL the exact same way as BOOLint, or any other data type.

// main.m
#import <Foundation/Foundation.h>
#import "Car.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Car *porsche = [[Car alloc] init];
        porsche.model = @"Porsche 911 Carrera";
        
        SEL stepOne = NSSelectorFromString(@"startEngine");
        SEL stepTwo = @selector(driveForDistance:);
        SEL stepThree = @selector(turnByAngle:quickly:);
        
        // This is the same as:
        // [porsche startEngine];
        [porsche performSelector:stepOne];

        // This is the same as:
        // [porsche driveForDistance:[NSNumber numberWithDouble:5.7]];
        [porsche performSelector:stepTwo
                      withObject:[NSNumber numberWithDouble:5.7]];
        
        if ([porsche respondsToSelector:stepThree]) {
            // This is the same as:
            // [porsche turnByAngle:[NSNumber numberWithDouble:90.0]
            //              quickly:[NSNumber numberWithBool:YES]];
            [porsche performSelector:stepThree
                          withObject:[NSNumber numberWithDouble:90.0]
                          withObject:[NSNumber numberWithBool:YES]];
        }
        NSLog(@"Step one: %@", NSStringFromSelector(stepOne));
    }
    return 0;
}

Selectors can be executed on an arbitrary object via performSelector:and related methods. The withObject: versions let you pass an argument (or two) to the method, but require those arguments to be objects. If this is too limiting for your needs, please see NSInvocationfor advanced usage. When you’re not sure if the target object defines the method, you should use the respondsToSelector: check before trying to perform the selector.

The technical name for a method is the primary method name concatenated with all of its parameter labels, separated by colons. This makes colons an integral aspect of method names, which can be confusing to Objective-C beginners. Their usage can be summed up as follows: parameterless methods never contain a colon, while methods that take a parameter always end in a colon.

A sample interface and implementation for the above Car class is included below. Notice how we have to use NSNumber instead of doublefor the parameter types, since the performSelector:withObject:method doesn’t let you pass primitive C data types.

// Car.h
#import <Foundation/Foundation.h>

@interface Car : NSObject

@property (copy) NSString *model;

- (void)startEngine;
- (void)driveForDistance:(NSNumber *)theDistance;
- (void)turnByAngle:(NSNumber *)theAngle
            quickly:(NSNumber *)useParkingBrake;

@end
// Car.m
#import "Car.h"

@implementation Car

@synthesize model = _model;

- (void)startEngine {
    NSLog(@"Starting the %@'s engine", _model);
}

- (void)driveForDistance:(NSNumber *)theDistance {
    NSLog(@"The %@ just drove %0.1f miles",
          _model, [theDistance doubleValue]);
}

- (void)turnByAngle:(NSNumber *)theAngle
            quickly:(NSNumber *)useParkingBrake {
    if ([useParkingBrake boolValue]) {
        NSLog(@"The %@ is drifting around the corner!", _model);
    } else {
        NSLog(@"The %@ is making a gentle %0.1f degree turn",
              _model, [theAngle doubleValue]);
    }
}

@end

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值