Objective-c selector学习

学习Objective-C,为了巩固知识,翻译如下,有不妥之处,敬请指点。

在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)

猜你喜欢

转载自justsee.iteye.com/blog/1620416