A.原因
您可能有一个包含循环,因为您也将 SomeClass.h 导入 DataProvider.h。这会导致未声明的标识符。
为什么会这样?举个例子:
// Foo.h
#import "Bar.h"
@interface Foo : NSObject
…// Do something with Bar
@end
// Bar.h
#import "Foo.h"
@interface Bar : NSObject
…// Do something with Foo
@end
如果你编译,比如说 Foo.h,预编译器会扩展这个:
他得到……:
// Foo.h
#import "Bar.h"
@interface Foo : NSObject
…// Do something with Bar
@end
... 导入 Bar.h (并剥离 cmets ...但让我们专注于主题。)...
// Foo.h
// Bar.h
#import "Foo.h"
@interface Bar : NSObject
…// Do something with Foo
@end
@interface Foo : NSObject
…// Do something with Bar
@end
Foo.h 不会再次被导入,因为它已经被导入了。最后:
// Bar.h
@interface Bar : NSObject
…// Do something with Foo
@end
@interface Foo : NSObject
…// Do something with Bar
@end
这很清楚:如果A依赖B,B依赖A,那么串行数据流作为一个文件,不可能同时让A在B之前,B在A之前。 (文件不是相对论的主题。)
B.解决方案
在大多数情况下,您应该给代码一个层次结构。 (有很多原因。没有进口问题是最不重要的问题之一。)在您的代码中,将 SomeClass.h 导入 DataProvider.h 看起来很奇怪。
遇到这样的问题是代码异味。尝试隔离并修复其原因。不要将代码片段移动到不同的位置以找到它工作的速度。这是抽奖。
C.结构
通常你有一个期望其他人遵守协议的类。举个例子:
// We declare the protocol here, because the class below expects from other classes to conform to the protocol.
@protocol DataSoure
…
@end
@interface Aggregator : NSObject
- (void)addDataSource:(id<DataSource>)dataSource
// We are using a protocol, because we do not want to restrict data sources to be subclass of a specific class.
// Therefore in this .h there cannot be an import of that – likely unknown - class
@end
SomeClass,符合协议的
#import "Aggregator.h"
@interface SomeClass:NSObject<DataSource>
…
@end