首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ARC与报文转发

ARC与报文转发
EN

Stack Overflow用户
提问于 2013-10-13 02:40:12
回答 1查看 210关注 0票数 1

我尝试实现消息转发。Xcode5,ARC开启,新的默认iPhone项目。I read a documentation here

我的项目中有两个自定义类:HelloWorld

代码语言:javascript
复制
#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

世界很简单:

代码语言:javascript
复制
#import <Foundation/Foundation.h>
@interface World : NSObject
    - (void) spin;
@end

#import "World.h"
@implementation World

- (void) spin {
    NSLog(@"spin around");
}

@end

在我的AppDelegate中,我写了三行简单的代码:

代码语言:javascript
复制
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进行消息转发?

EN

回答 1

Stack Overflow用户

发布于 2013-10-13 07:34:35

尝试将我声明为id:

代码语言:javascript
复制
 id me = [[Hello alloc] init];
 [me say];
 [me spin];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19337803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档