objective-c 交叉引用问题的解决方案

objective-c 不允许交叉引用,上代码:

A.h

#import <Foundation/Foundation.h>
#import "B.h"

@interface A : NSObject {
    B* _b; // 报错点 1~
}

-(void) test:(B*)b; // 报错点 2~

@end

A.mm

#import "A.h"

@implementation A

-(void) test:(B*)b {
    NSLog(@"test");
}

@end

B.h

#import <Foundation/Foundation.h>
#import "A.h"

@interface B : NSObject {
    A* _a; // 报错点 3~
}

@end

B.mm

#import "B.h"

@implementation B

@end


解决方案:

头文件中不要包含彼此的头文件,将成员变量类型、方法参数类型改为由具体的类名 (A*,B*)  改为 id

.mm 实现文件中包含彼此的头文件不会出错,that's all!

posted on 2012-08-09 00:14  yang3wei  阅读(330)  评论(0编辑  收藏  举报