Instance Method インスタンスメソッド

forwardInvocation:

Overridden by subclasses to forward messages to other objects. これは、メッセージを別のオブジェクトへ転送するためにサブクラスによってオーバーライドされます。

Declaration 宣言

- (void)forwardInvocation:(NSInvocation *)anInvocation;

Parameters パラメータ

anInvocation

The invocation to forward. この呼び出しを転送します。

Discussion 解説

When an object is sent a message for which it has no corresponding method, the runtime system gives the receiver an opportunity to delegate the message to another receiver. あるオブジェクトが、それが対応するメソッドを持たないメッセージを送られる場合、ランタイムシステムは、メッセージを別のレシーバに委任する機会をそのレシーバに与えます。 It delegates the message by creating an NSInvocation object representing the message and sending the receiver a forwardInvocation: message containing this NSInvocation object as the argument. The receiver’s forwardInvocation: method can then choose to forward the message to another object. (If that object can’t respond to the message either, it too will be given a chance to forward it.) (そのオブジェクトもまたメッセージに応答出来ないならば、それもまたそれを転送する機会を与えられます。)

The forwardInvocation: message thus allows an object to establish relationships with other objects that will, for certain messages, act on its behalf. The forwarding object is, in a sense, able to “inherit” some of the characteristics of the object it forwards the message to. 転送先のオブジェクトは、ある意味では、メッセージの転送元のオブジェクトのいくつかの特徴を「継承する」ことができます。

An implementation of the forwardInvocation: method has two tasks:

  • To locate an object that can respond to the message encoded in anInvocation. This object need not be the same for all messages. このオブジェクトは、全てのメッセージに対して同じものである必要はありません。

  • To send the message to that object using anInvocation. anInvocation will hold the result, and the runtime system will extract and deliver this result to the original sender.

In the simple case, in which an object forwards messages to just one destination (such as the hypothetical friend instance variable in the example below), a forwardInvocation: method could be as simple as this:


- (void)forwardInvocation:(NSInvocation *)invocation
{
    SEL aSelector = [invocation selector];
 
    if ([friend respondsToSelector:aSelector])
        [invocation invokeWithTarget:friend];
    else
        [super forwardInvocation:invocation];
}

The message that’s forwarded must have a fixed number of arguments; variable numbers of arguments (in the style of printf()) are not supported.

The return value of the forwarded message is returned to the original sender. 転送されたメッセージの戻り値は、元々の送り手(センダー)に返されます。 All types of return values can be delivered to the sender: id types, structures, double-precision floating-point numbers.

Implementations of the forwardInvocation: method can do more than just forward messages. forwardInvocation: can, for example, be used to consolidate code that responds to a variety of different messages, thus avoiding the necessity of having to write a separate method for each selector. A forwardInvocation: method might also involve several other objects in the response to a given message, rather than forward it to just one.

NSObject’s implementation of forwardInvocation: simply invokes the doesNotRecognizeSelector: method; it doesn’t forward any messages. Thus, if you choose not to implement forwardInvocation:, sending unrecognized messages to objects will raise exceptions.

See Also 参照

Forwarding Messages メッセージの転送