Manni
Manni

Reputation: 11148

Objective-C: Forwarding messages in iOS

Is it possible to use "Forwarding" (http://en.wikipedia.org/wiki/Objective-C#Forwarding) in iOS?

I tried the following code I found here:

- (retval_t) forward: (SEL) sel : (arglist_t) args

But I get an error message:

error: expected ')' before 'retval_t'

So I read here and try:

- (retval_t)forward:(SEL)sel args:(arglist_t) args

But I got the same error message...

What am I doing wrong? Do I need to import something?


@bbum: I try to create a thread safe NSMutableArray and want to use the code I found here: NSMutableDictionary thread safety

Upvotes: 0

Views: 2416

Answers (2)

Yup.
Yup.

Reputation: 1893

Here's the pattern I use for iOS forwardInvocation (message forwarding):

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    if ([self.recipient respondsToSelector:[anInvocation selector]]) {
        [anInvocation invokeWithTarget:self.recipient];
    }
    else {
        [super forwardInvocation:anInvocation];
    }
}

It is important to note that the return value of the message that’s forwarded is automatically returned to the original sender.

Upvotes: 1

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

Yes. What you're seeing there is the GNU variant of Objective-C, which is slightly different from the Apple variant.

You want to use -forwardInvocation: rather than -forward:: or -forward:args:. See http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html for more info on how to do it with iOS.

Upvotes: 5

Related Questions