想象一下,我有两个标头文件:SomeFileA.h
和SomeFileB.h
SomeFileA.h
包括SomeFileB.h
,SomeFileB.h
包含SomeFileA.h
。
这会创建一个循环并混淆编译器。我们怎么能克服这个?
答案 0 :(得分:8)
你应该“转发声明”你的课程。这告诉编译器该类是否存在,但不需要实际导入它。
SomeFileA.h
@class SomeFileB // <-- This "forward declares SomeFileB"
@interface SomeFileA
@property (nonatomic, strong) SomeFileB *someFileB;
...
@end
SomeFileA.m
#import "SomeFileB.h"
@implementation SomeFileA
...
@end
同样的事情,但在SomeFileB的另一种方式
SomeFileB.h
@class SomeFileA // <-- This "forward declares SomeFileA"
@interface SomeFileB
@property (nonatomic, strong) SomeFileA *someFileA;
...
@end
SomeFileB.m
#import "SomeFileA.h"
@implementation SomeFileB
...
@end
如果您不在标题中使用某个类,则无需转发声明它。
@interface SomeFileA
//I took out the property for SomeFileB.. no need for the @class anymore.
...
@end