想象一下,我有两个头文件:SomeFileA.hSomeFileB.hSomeFileA.h包括SomeFileB.hSomeFileB.h包括SomeFileA.h

这将创建一个循环并混淆编译器。我们该如何克服呢?

最佳答案

您应该“预先声明”您的 class 。这告诉编译器该类存在,但无需实际导入。

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

关于ios - 如何克服#import循环?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31830536/

10-13 04:29