在DataProvider.h
@protocol NewDataProviderProtocol
- (void)fetchNewData;
@end
在SomeClass
#import DataProvider.h
@interface SomeClass :NSObject <NewDataProviderProtocol>
@end
当我尝试使 SomeClass 符合 NewDataProviderProtocol 时,它说,
没有名为“NewDataProviderProtocol”的类型或协议(protocol)
这很奇怪,因为我已经导入了声明协议(protocol)的 header DataProvider.h。
所以我在 SomeClass 的接口(interface)之前转发声明了 NewDataProviderProtocol 但 xcode 警告
Cannot find definition for **NewDataProviderProtocol**
这是什么原因和解决方法?
最佳答案
一个。原因
您可能有一个包含循环,因为您也将 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(并删除注释…但让我们关注主题。)…
// 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 看起来很奇怪。
有这样的问题就是code smell。尝试隔离并修复其原因。不要将代码块移动到不同的位置以找到它工作的节奏。这是代码彩票。
C.结构
通常你有一个期望其他人遵守协议(protocol)的类。让我们举个例子:
// 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,符合协议(protocol)
#import "Aggregator.h"
@interface SomeClass:NSObject<DataSource>
…
@end
关于ios - 协议(protocol)构造错误 Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37065472/