`
zsjg13
  • 浏览: 142440 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

Message Forwarding

 
阅读更多

The object type can either be specified in the code and statically bound at compile time (static typing) or be unspecified with its type resolved at runtime (dynamic typing). In either case, at runtime, the receiving object interprets the message to determine which method to invoke.

 

This runtime resolution of method calls makes it easy to change and/or extend programs dynamically, but also carries with it a certain risk: it permits a program to send a message to an object that may not have a corresponding method attached to it. Under the default scenario, if this happens, a runtime exception is thrown.

 

However, Objective-C provides another option: through a mechanism called  message forwarding , it is possible to configure an object to perform user-defined processing when it receives a message not mapped to its set of methods.

 

1、Forwarding Options

Objective-C provides two types of message forwarding options that you can use.

 

Fast forwarding: Classes that descend from NSObject can implement fast forwarding by overriding the NSObject forwardingTargetForSelector: method to forward the method to another object. This technique makes it appear like the implementations of your object and the forwarding object are combined.This simulates the behavior of multiple inheritance of class implementations.It works well if you have a target class that defines all the possible messages that your object can consume.

 

Normal (full) forwarding:Classes that descend from NSObject can implement normal forwarding by overriding the NSObject forwardInvocation: method. This technique enables your object to use the full contents of the message  (target,method name, parameters).

 

Fast forwarding works well if you have a target class that defines all the possible messages that
your object can consume. Full fowarding should be used if you don’t have such a target class or you
would like to perform other processing on message receipt (for example, just logging and swallowing
the message).

 

 

 

 

 

分享到:
评论

相关推荐

    iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (类,对象,方法,消息传递,消息转发).zip

    iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (类,对象,方法,消息传递,消息转发).zip,iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (类,对象,方法,消息传递,消息转发)

    ObjCRuntimeGuide

    - **Message Forwarding:** If an object receives a message that does not match any of its methods, the runtime system can forward the message to another object. This process involves a series of steps,...

    WSO2 ESB 协议转换示例(http to jms)

    - 使用Message Processor,如Scheduled Message Forwarding Processor (SMFP),将消息从Message Store取出并发送到JMS队列或主题。 按照以下步骤配置WSO2 ESB: 1. **创建Proxy Service**: 在ESB管理控制台中,...

    forwardingTarget 消息转发.zip

    在Swift编程中,消息转发(Message Forwarding)是Objective-C运行时系统的一个重要特性,它允许我们处理那些未被接收者直接实现的消息。Swift虽然基于更安全的静态类型系统,但仍然支持这一机制,以便在运行时进行...

    ios-对象的消息转发机制.zip

    在iOS的Objective-C编程中,消息转发(Message Forwarding)是一个关键的概念,它涉及到对象如何处理无法识别的消息。当你向一个对象发送一个它不理解的消息时,系统会启动一系列的挽救措施,这就是消息转发机制。这...

    MessageForwardingTest

    在iOS和Objective-C编程中,"消息转发"(Message Forwarding)是一个至关重要的概念,它涉及到对象如何处理那些无法直接响应的消息。在这个“MessageForwardingTest”项目中,我们主要探讨的是如何有效地利用消息...

    iOS中Runtime的几种基本用法记录

    4)**消息转发(Message Forwarding)** 如果消息未能在第一步通过`resolveInstanceMethod`解决,Objective-C会进入消息转发流程。首先,对象会尝试通过`forwardingTargetForSelector:`方法指定另一个对象处理消息...

    Objc4--750

    这涉及到方法选择(method lookup)和消息转发(message forwarding)机制。 3. **内存管理**:runtime提供了自动引用计数(ARC)和垃圾回收(GC)机制,用于管理对象的生命周期和内存分配。 4. **分类与协议**:...

    Runtime 动态添加方法(2).zip

    3. **消息转发(Message Forwarding)**: 如果动态方法解析失败,运行时系统会尝试消息转发。这包括两次机会:第一次是`-forwardInvocation:`方法,允许接收者有机会自己处理消息;第二次是完整的消息转发,将消息...

    iOS工程师面试题.pdf

    - **消息转发(Message Forwarding)**:当接收到无法处理的消息时,对象有机会将消息转发给其他对象。 - **Delegate**:通过委托模式,一个对象可以实现另一个对象的协议,从而获得类似多继承的功能。 - **Protocol...

    iOS runtimeIMP指针和消息转发机制Demo

    3. **消息转发(Message Forwarding)**: 当对象接收到一个它无法处理的消息时,Objective-C的消息转发机制会被触发。这个机制分为两个阶段: - 第一阶段:`-forwardingTargetForSelector:`,对象有机会将消息...

    iOS Runtime常见问题分析_周辉1

    8. **消息转发(Message Forwarding)**:当对象接收到无法处理的消息时,Runtime会启动消息转发机制,这包括`forwardInvocation:`和`methodSignatureForSelector:`方法,使得开发者有机会处理未定义的方法调用。...

    ios-Runtime的实际使用场景.zip

    1. **消息转发(Message Forwarding)**: 当一个对象接收到它无法处理的消息时,Runtime系统会提供一次机会进行消息转发。通过实现`forwardInvocation:`方法,我们可以自定义消息的处理方式,例如将消息转发到其他...

    iOS方法执行顺序

    4. **消息转发(Message Forwarding)**: 当消息无法被解析且未动态解析时,运行时系统会启动消息转发机制。首先,对象有机会通过`-forwardInvocation:`方法处理消息。如果此方法未实现,或者返回失败,那么消息会...

    精通WebSphere MQ标准 [ 直接下载 ]

    5. 转发引擎(Message Forwarding Engine):处理消息的路由和传递。 二、WebSphere MQ配置 1. 配置队列管理器:定义队列管理器的属性,如名称、日志文件位置、消息存储位置等。 2. 配置队列:根据应用需求创建不同...

    NSObject源码.zip

    1. **消息转发(Message Forwarding)**:当一个对象收到它无法响应的消息时,`NSObject`提供了一套机制来处理这种情况。首先,`forwardInvocation:`方法会被调用,允许对象有机会处理或转发消息。如果对象选择不...

    IOS消息转发

    在iOS开发中,消息转发(Message Forwarding)是Objective-C中的一个重要机制,它允许对象处理那些它不直接响应的消息。这个机制使得类可以捕获并处理未知的消息,从而增加了程序的灵活性和健壮性。本篇文章将深入...

    简单的一些非经典的objc设计模式

    6. **消息转发(Message Forwarding)** 当对象接收到未知消息时,Objective-C的消息转发机制允许对象有机会处理未定义的方法。这可以用来实现类似Java的动态方法调用,或者在运行时决定如何处理未知消息。 7. **...

Global site tag (gtag.js) - Google Analytics