iOS 消息(即方法调用)的两个隐藏参数 :self 和 _cmd

简介: iOS 消息(即方法调用)的两个隐藏参数 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。

iOS 消息(即方法调用)的两个隐藏参数

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


以下摘自《Objective-C Runtime Programming Guide


使用隐藏参数 
Using Hidden Arguments

When objc_msgSend finds the procedure that implements a method, it calls the procedure and passes it all the arguments in the message. It also passes the procedure two hidden arguments:

  • The receiving object

  • The selector for the method

These arguments give every method implementation explicit information about the two halves of the message expression that invoked it. They’re said to be “hidden” because they aren’t declared in the source code that defines the method. They’re inserted into the implementation when the code is compiled.

尽管这些参数不是显式声明的,源码仍能引用它们(正像它能引用接收对象的实例变量一样)。每个方法都把消息接收对象称作 self,而自身的选择器称作_cmd。下面的示例中,_cmd 引用strange 方法的选择器,而self 引用接收 strange 消息的对象。
Although these arguments aren’t explicitly declared, source code can still refer to them (just as it can refer to the receiving object’s instance variables). A method refers to the receiving object as self, and to its own selector as _cmd. In the example below, _cmd refers to the selector for the strange method and self to the object that receives a strange message.

- strange
{
    id  target = getTheReceiver();
    SEL method = getTheMethod();
 
    if ( target == self || method == _cmd )
        return nil;
    return [target performSelector:method];
}

self is the more useful of the two arguments. It is, in fact, the way the receiving object’s instance variables are made available to the method definition.



doesNotRecognizeSelector:

处理接收者无法识别的消息。
Handles messages the receiver doesn’t recognize.

- (void)doesNotRecognizeSelector:(SEL)aSelector

参数 Parameters

aSelector

一个 selector 用于标识未被接收者实现和不能被接收者识别的方法。
A selector that identifies a method not implemented or recognized by the receiver.

讨论 Discussion

无论何时一个对象
The runtime system invokes this method whenever an object receives an aSelector message it can’t respond to or forward. This method, in turn, raises anNSInvalidArgumentException, and generates an error message.

Any doesNotRecognizeSelector: messages are generally sent only by the runtime system. However, they can be used in program code to prevent a method from being inherited. For example, anNSObject subclass might renounce the copy orinit method by re-implementing it to include adoesNotRecognizeSelector: message as follows:

- (id)copy


{


    [self doesNotRecognizeSelector:_cmd];


}


The _cmd variable is a hidden argument passed to every method that is the current selector; in this example, it identifies the selector for thecopy method. This code prevents instances of the subclass from responding tocopy messages or superclasses from forwardingcopy messages—although respondsToSelector: will still report that the receiver has access to acopy method.

If you override this method, you must call super or raise anNSInvalidArgumentException exception at the end of your implementation. In other words, this method must not return normally; it must always result in an exception being thrown.


目录
相关文章
|
12月前
|
iOS开发
iOS URL参数转字典
iOS URL参数转字典
133 0
|
12月前
|
JSON 移动开发 数据格式
iOS url传递JSON格式参数方法
iOS url传递JSON格式参数方法
223 0
|
iOS开发
iOS开发-聊天气泡的绘制和聊天消息列表
iOS开发-聊天气泡的绘制和聊天消息列表
174 0
iOS开发-聊天气泡的绘制和聊天消息列表
|
iOS开发 Python
iOS小技能:lldb打印block参数签名
iOS逆向时经常会遇到参数为block类型,本文介绍一个lldb script,可快速打印出Objective-C方法中block参数的类型。
159 0
iOS小技能:lldb打印block参数签名
|
存储 iOS开发
iOS小技能: get 和post 布尔值参数处理、按照时间分页的数据重复的处理
1. get 和post 布尔值参数处理:如果后台Bool 参数没有同时支持【 0,1】 ;和【 true false】,get请求的时候就需要特殊处理。 2. 按照时间分页的数据重复的处理
145 0
iOS小技能: get 和post 布尔值参数处理、按照时间分页的数据重复的处理
|
编译器 iOS开发
IOS越狱开发(二)———APP开机自动启动并隐藏图标
IOS越狱开发(二)———APP开机自动启动并隐藏图标
252 0
IOS越狱开发(二)———APP开机自动启动并隐藏图标
|
iOS开发
iOS 状态栏的隐藏显示与状态栏样式的设置
iOS 状态栏的隐藏显示与状态栏样式的设置
924 0
iOS 状态栏的隐藏显示与状态栏样式的设置
|
iOS开发
IOS正确解决隐藏导航栏后push、pop闪黑问题
IOS正确解决隐藏导航栏后push、pop闪黑问题
556 0
|
存储 缓存 开发者
iOS-底层原理 14:消息流程分析之 动态方法决议 & 消息转发
iOS-底层原理 14:消息流程分析之 动态方法决议 & 消息转发
273 0
iOS-底层原理 14:消息流程分析之 动态方法决议 & 消息转发
|
缓存 算法 C++
iOS-底层原理 13:消息流程分析之慢速查找
iOS-底层原理 13:消息流程分析之慢速查找
123 0
iOS-底层原理 13:消息流程分析之慢速查找