我尝试实现消息转发。Xcode5,ARC开启,新的默认iPhone项目。I read a documentation here
我的项目中有两个自定义类:Hello和World。
#import <Foundation/Foundation.h>
@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世界很简单:
#import <Foundation/Foundation.h>
@interface World : NSObject
- (void) spin;
@end
#import "World.h"
@implementation World
- (void) spin {
NSLog(@"spin around");
}
@end在我的AppDelegate中,我写了三行简单的代码:
Hello *me = [[Hello alloc] init];
[me say];
[me spin];编译器给了我一个错误:AppDelegate.m:23:9: No visible @interface for 'Hello' declares the selector 'spin',并且没有构建项目。当我重新输入它时:[me performSelector:@selector(spin)]; -它工作得很好。
仅当ARC关闭时,Code [me spin]才起作用(但编译器会生成警告AppDelegate.m:23:9: 'Hello' may not respond to 'spin')。
我的问题:为什么?如何使用ARC进行消息转发?
发布于 2013-10-13 07:34:35
尝试将我声明为id:
id me = [[Hello alloc] init];
[me say];
[me spin];https://stackoverflow.com/questions/19337803
复制相似问题