目标C中的消息转发 [英] Message Forwarding in Objective C

查看:78
本文介绍了目标C中的消息转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以简要说明如何使用邮件转发吗?

Can anyone give a brief explanation of how to use message forwarding?

链接

  • Apple文档:Apple文档往往可以作为参考,但是篇幅太长,不能作为最佳的介绍.
  • Apple documentation: Apple documentation tends to be good as a reference, but lengthy enough to not be the best as an introduction.

推荐答案

简单的委派模式:您的对象响应消息aMethod,然后通过发送[otherObject responsesToSelector:@selector( aMethod)],则返回布尔值.如果otherObject确实如此,那么您就很清楚发送消息了.

Simple delegation pattern: your object responds to the message aMethod, then it check if some other object responds to the message aMethod by sending [otherObject respondsToSelector:@selector(aMethod)], which returns a bool. If otherObject does, you're all clear to send the message.

更多技术优势NSInvocation方法:如果向您的对象发送了无法响应的消息(crazyMethodName),则在您的对象上调用forwardInvocation. NSObject的forwardInvocation的默认实现仅调用dosNotRecognizeSelector,因为您的对象无法识别选择器.您可以通过检查另一个对象是否响应该调用的选择器,并在另一个对象上调用该调用来覆盖forwardInvocation的默认实现.

More technical goodness NSInvocation method: if your object is sent a message it can't respond to (crazyMethodName), then forwardInvocation is called on your object. The default implementation of forwardInvocation for NSObject just calls doesNotRecognizeSelector because, well, your object doesn't recognize the selector. You can override the default implementation of forwardInvocation by checking if another object responds to the selector of the invocation, and invoking that invocation on the other object if so.

这篇关于目标C中的消息转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆