Ios 如何克服导入循环?

Ios 如何克服导入循环?,ios,objective-c,Ios,Objective C,假设我有两个头文件:SomeFileA.h和SomeFileB.h SomeFileA.h包括SomeFileB.h,而SomeFileB.h包括SomeFileA.h 这会创建一个循环并使编译器感到困惑。我们如何克服这个问题?您应该“向前声明”您的类。这会告诉编译器类存在,但不需要实际导入它 某物 @class SomeFileB // <-- This "forward declares SomeFileB" @interface SomeFileA @property (nona

假设我有两个头文件:
SomeFileA.h
SomeFileB.h

SomeFileA.h
包括
SomeFileB.h
,而
SomeFileB.h
包括
SomeFileA.h

这会创建一个循环并使编译器感到困惑。我们如何克服这个问题?

您应该“向前声明”您的类。这会告诉编译器类存在,但不需要实际导入它

某物

@class SomeFileB  // <-- This "forward declares SomeFileB"
@interface SomeFileA

@property (nonatomic, strong) SomeFileB *someFileB;
...
@end

同样的事情,但在SomeFileB中的情况正好相反

SomeFileB.h

@class SomeFileA  // <-- This "forward declares SomeFileA"
@interface SomeFileB

@property (nonatomic, strong) SomeFileA *someFileA;
...
@end

如果不在标题中使用类,则不需要向前声明它。

@interface SomeFileA
//I took out the property for SomeFileB.. no need for the @class anymore.
...
@end

在.h中转发声明并在.m中导入!!如果您使用的是
#import
,那么就不会有问题。你是不是因为这个“循环”导致了一个错误?@DanF#import get的循环导入也是如此,尽管有苹果的文档。
#import "SomeFileA.h"
@implementation SomeFileB
...
@end
@interface SomeFileA
//I took out the property for SomeFileB.. no need for the @class anymore.
...
@end