Note that there are some explanatory texts on larger screens.

plurals
  1. POARC and Message Forwarding
    text
    copied!<p>I tried to implement Message Forwarding. Xcode 5, ARC is ON, new default iPhone project. <a href="https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html" rel="nofollow">I read a documentation here</a> </p> <p>I have two custom classes in my project: <code>Hello</code> and <code>World</code>.</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Hello : NSObject - (void) say; @end #import "Hello.h" #import "World.h" @implementation Hello - (void) say { NSLog(@"hello!"); } -(void)forwardInvocation:(NSInvocation *)invocation { NSLog(@"forward invocation"); World *w = [[World alloc] init]; if ([w respondsToSelector:[invocation selector]]) { [invocation invokeWithTarget:w]; } else { [self doesNotRecognizeSelector: [invocation selector]]; } } -(NSMethodSignature*)methodSignatureForSelector:(SEL)selector { NSLog(@"method signature"); NSMethodSignature *signature = [super methodSignatureForSelector:selector]; if (! signature) { World *w = [[World alloc] init]; signature = [w methodSignatureForSelector:selector]; } return signature; } @end </code></pre> <p>World is simple:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface World : NSObject - (void) spin; @end #import "World.h" @implementation World - (void) spin { NSLog(@"spin around"); } @end </code></pre> <p>In my AppDelegate i wrote three simple lines:</p> <pre><code>Hello *me = [[Hello alloc] init]; [me say]; [me spin]; </code></pre> <p>And compiler give me an error: <code>AppDelegate.m:23:9: No visible @interface for 'Hello' declares the selector 'spin'</code> and doesn't build a project. When I retype it: <code>[me performSelector:@selector(spin)];</code> - it works fine.</p> <p>Code <code>[me spin]</code> works when ARC is OFF only (but compiler generates a warning <code>AppDelegate.m:23:9: 'Hello' may not respond to 'spin'</code>).</p> <p>My questions: why? and How can I use ARC with message forwarding?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload