iOS 消息(即方法调用)的两个隐藏参数 :self 和 _cmd
2014-05-08 08:43
591 查看
iOS消息(即方法调用)的两个隐藏参数
太阳火神的美丽人生(http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
转载请保留此句:太阳火神的美丽人生- 本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
以下摘自《Objective-C
RuntimeProgrammingGuide》
使用隐藏参数
UsingHiddenArguments
When
theprocedurethatimplementsamethod,itcallstheprocedureandpassesitalltheargumentsinthemessage.Italsopassestheproceduretwohiddenarguments:
Thereceivingobject
Theselector forthemethod
Theseargumentsgiveeverymethodimplementationexplicitinformationaboutthetwohalvesofthemessageexpressionthatinvokedit.They’resaidtobe“hidden”becausetheyaren’tdeclaredinthesourcecodethatdefinesthemethod.They’reinsertedinto
theimplementationwhenthecodeiscompiled.
尽管这些参数不是显式声明的,源码仍能引用它们(正像它能引用接收对象的实例变量一样)。每个方法都把消息接收对象称作 self,而自身的选择器称作_cmd。下面的示例中,_cmd 引用strange 方法的选择器,而self 引用接收
strange 消息的对象。
Althoughtheseargumentsaren’texplicitlydeclared,sourcecodecanstillrefertothem(justasitcanrefertothereceivingobject’sinstancevariables).Amethodreferstothereceivingobjectas
andtoitsownselectoras
Intheexamplebelow,
theobjectthatreceivesa
doesNotRecognizeSelector:
处理接收者无法识别的消息。
Handlesmessagesthereceiverdoesn’trecognize.
-(void)doesNotRecognizeSelector:(SEL)aSelector
参数Parameters
aSelector
一个 selector用于标识未被接收者实现和不能被接收者识别的方法。
Aselectorthatidentifiesamethodnotimplementedorrecognizedbythereceiver.
讨论 Discussion
无论何时一个对象
TheruntimesysteminvokesthismethodwheneveranobjectreceivesanaSelectormessageitcan’trespondtoorforward.Thismethod,inturn,raisesanNSInvalidArgumentException,
andgeneratesanerrormessage.
AnydoesNotRecognizeSelector:messagesaregenerallysentonlybytheruntimesystem.However,theycanbeusedinprogramcodetopreventamethodfrombeinginherited.Forexample,anNSObjectsubclass
mightrenouncethecopyorinitmethodbyre-implementingittoincludeadoesNotRecognizeSelector:messageasfollows:
The_cmdvariableisahiddenargumentpassedtoeverymethodthatisthecurrentselector;inthisexample,itidentifiestheselectorforthecopymethod.Thiscodepreventsinstancesofthesubclass
fromrespondingtocopymessagesorsuperclassesfromforwardingcopymessages—although
respondsToSelector:willstillreportthatthereceiverhasaccesstoacopymethod.
Ifyouoverridethismethod,youmustcallsuperorraiseanNSInvalidArgumentException
exceptionattheendofyourimplementation.Inotherwords,thismethodmustnotreturnnormally;itmustalwaysresultinanexceptionbeingthrown.
太阳火神的美丽人生(
本文遵循“
以下摘自《Objective-C
RuntimeProgrammingGuide》
UsingHiddenArguments
When
findsobjc_msgSend
theprocedurethatimplementsamethod,itcallstheprocedureandpassesitalltheargumentsinthemessage.Italsopassestheproceduretwohiddenarguments:
Thereceivingobject
Theselector forthemethod
Theseargumentsgiveeverymethodimplementationexplicitinformationaboutthetwohalvesofthemessageexpressionthatinvokedit.They’resaidtobe“hidden”becausetheyaren’tdeclaredinthesourcecodethatdefinesthemethod.They’reinsertedinto
theimplementationwhenthecodeiscompiled.
尽管这些参数不是显式声明的,源码仍能引用它们(正像它能引用接收对象的实例变量一样)。每个方法都把消息接收对象称作 self,而自身的选择器称作_cmd。下面的示例中,_cmd 引用strange 方法的选择器,而self 引用接收
strange 消息的对象。
Althoughtheseargumentsaren’texplicitlydeclared,sourcecodecanstillrefertothem(justasitcanrefertothereceivingobject’sinstancevariables).Amethodreferstothereceivingobjectas
self,
andtoitsownselectoras
_cmd.
Intheexamplebelow,
_cmdreferstotheselectorforthe
strangemethodand
selfto
theobjectthatreceivesa
strangemessage.
-strange |
{ |
idtarget=getTheReceiver(); |
SELmethod=getTheMethod(); |
if(target==self||method==_cmd) |
returnnil; |
return[targetperformSelector:method]; |
} |
selfisthemoreusefulofthetwoarguments.Itis,infact,thewaythereceivingobject’sinstancevariablesaremadeavailabletothemethoddefinition.
doesNotRecognizeSelector:
处理接收者无法识别的消息。
Handlesmessagesthereceiverdoesn’trecognize.
-(void)doesNotRecognizeSelector:(SEL)aSelector
参数Parameters
aSelector
一个 selector用于标识未被接收者实现和不能被接收者识别的方法。
Aselectorthatidentifiesamethodnotimplementedorrecognizedbythereceiver.
讨论 Discussion
无论何时一个对象
TheruntimesysteminvokesthismethodwheneveranobjectreceivesanaSelectormessageitcan’trespondtoorforward.Thismethod,inturn,raisesanNSInvalidArgumentException,
andgeneratesanerrormessage.
AnydoesNotRecognizeSelector:messagesaregenerallysentonlybytheruntimesystem.However,theycanbeusedinprogramcodetopreventamethodfrombeinginherited.Forexample,anNSObjectsubclass
mightrenouncethecopyorinitmethodbyre-implementingittoincludeadoesNotRecognizeSelector:messageasfollows:
-(id)copy |
{ |
[selfdoesNotRecognizeSelector:_cmd]; |
} |
fromrespondingtocopymessagesorsuperclassesfromforwardingcopymessages—although
respondsToSelector:willstillreportthatthereceiverhasaccesstoacopymethod.
Ifyouoverridethismethod,youmustcallsuperorraiseanNSInvalidArgumentException
exceptionattheendofyourimplementation.Inotherwords,thismethodmustnotreturnnormally;itmustalwaysresultinanexceptionbeingthrown.
相关文章推荐
- iOS应用安全开发,你不知道的那些事
- iOS缓存类的设计
- IOS画图
- ios开发之启动页面
- 在iOS工程中组合使用ARC和非ARC
- IOS CoreText.framework --- 基本用法
- iOS真机测试
- iOS Foundation 框架基类
- iOS 使用libx264编码视频
- ios 配置颜色
- ios 关于问题 no matching provisioning profiles found
- ios存储之plist
- iOS动画效果
- iOS7教程系列:自定义导航转场动画以及更多
- 创建相应的ios静态库
- IOS NSPredicate 用于字符串匹配 可检验手机号、邮箱、时间戳等 MARK
- IOS , plist 配置项说明
- iOS OpenGLES 框架相关 24 篇文档排序整理
- 25个增强iOS应用程序性能的提示和技巧
- Xcode 与 iOS SDK 版本