“找不到接口声明错误……”在@class之后。

时间:2022-09-07 09:24:52

I've run into an Objective-C problem that doesn't seem to make any sense to me. I am a relatively well-versed ObjC programmer and understand the whole "forward declaration" concept, but this one has me scratching my head. So here's the deal:

我遇到了一个Objective-C的问题对我来说毫无意义。我是一个相对精通ObjC的程序员,理解整个“forward declaration”的概念,但是这个让我摸不着头脑。这里的交易:

ClassA is a class in my Xcode project that is project-only. ClassB is a subclass of ClassA which is public and is imported into my framework's header.

ClassA是我的Xcode项目中的一个只针对项目的类。ClassB是ClassA的一个子类,它是公共的,并被导入到我的框架的标题中。

I am getting a "Cannot find interface declaration for 'ClassA', superclass of 'ClassB'..." error when building. I have already put the @class ClassA; forward declaration in ClassB.h, but that does not seem to solve the problem. When I change the @class ClassA; to #import ClassA.h, it works fine. However, since ClassA is project-only, dependent projects cannot build ClassB because it cannot access ClassA's source.

我得到一个“无法找到‘ClassA’的接口声明,‘ClassB’的超类……”在构建时出错。我已经写了@class ClassA;前锋在ClassB宣言。h,但这似乎不能解决问题。当我更改@ ClassA类时;# ClassA导入。h,它将正常工作。但是,由于ClassA只是项目,依赖的项目不能构建ClassB,因为它不能访问ClassA的源。

Any help would be appreciated and I hope that makes sense. Thanks!

任何帮助都将被感激,我希望这是有意义的。谢谢!

6 个解决方案

#1


10  

To subclass a class, the superclass's full declaration must be available to the compiler. @class just enables references to instances of said class -- allows for A *foo;.

要子类化一个类,必须向编译器提供超类的完整声明。@class只允许引用类的实例——允许使用*foo;

Subclassing requires more metadata (at least, it did under the legacy runtime -- I think it might be possible to support subclassing without the full super's @interface. Technically possible, but probably not useful.)

子类化需要更多的元数据(至少在遗留运行时下是这样做的——我认为,在没有完整的super的@interface的情况下,可能支持子类化。技术上可能,但可能没用。

#2


37  

The problem is that you have an infinite loop in your #imports. The solution: all #imports go in the implementation file and all classes needed are declared in the .h files.

问题是在#导入中有一个无限循环。解决方案:所有#导入都进入实现文件,所有需要的类都在.h文件中声明。

#3


6  

I have an answer: You must check your '#import' order. Before you use the superclass it should be imported and compiled.

我有一个答案:你必须检查你的'#import'订单。在使用超类之前,应该导入并编译它。

#4


1  

I had an issue where I was using categories in a superclass method and was getting this inheritance error. Once I moved the categories .h imports to the superclass .m file thing started getting better.

我有一个问题,我在超类方法中使用类别,并且得到了这个继承错误。一旦我将类别.h导入到超类.m文件,事情开始变得更好。

#5


1  

Just carry out all possible headers from .h to .m file of superclass and see which one is the matter of the problem. I'm sure that it's one of common headers used in both classes.

只需要执行所有可能的超类.h到.m文件的头文件,看看哪个是问题所在。我确信它是两个类中使用的一个通用头文件。

#6


0  

Like @Igor said, the order of imports matters:

如@Igor所说,进口顺序很重要:

I had to change

我不得不改变

#import <KeychainItemWrapper/KeychainItemWrapper.h>
#import <Foundation/Foundation.h>

to

#import <Foundation/Foundation.h>
#import <KeychainItemWrapper/KeychainItemWrapper.h>

#1


10  

To subclass a class, the superclass's full declaration must be available to the compiler. @class just enables references to instances of said class -- allows for A *foo;.

要子类化一个类,必须向编译器提供超类的完整声明。@class只允许引用类的实例——允许使用*foo;

Subclassing requires more metadata (at least, it did under the legacy runtime -- I think it might be possible to support subclassing without the full super's @interface. Technically possible, but probably not useful.)

子类化需要更多的元数据(至少在遗留运行时下是这样做的——我认为,在没有完整的super的@interface的情况下,可能支持子类化。技术上可能,但可能没用。

#2


37  

The problem is that you have an infinite loop in your #imports. The solution: all #imports go in the implementation file and all classes needed are declared in the .h files.

问题是在#导入中有一个无限循环。解决方案:所有#导入都进入实现文件,所有需要的类都在.h文件中声明。

#3


6  

I have an answer: You must check your '#import' order. Before you use the superclass it should be imported and compiled.

我有一个答案:你必须检查你的'#import'订单。在使用超类之前,应该导入并编译它。

#4


1  

I had an issue where I was using categories in a superclass method and was getting this inheritance error. Once I moved the categories .h imports to the superclass .m file thing started getting better.

我有一个问题,我在超类方法中使用类别,并且得到了这个继承错误。一旦我将类别.h导入到超类.m文件,事情开始变得更好。

#5


1  

Just carry out all possible headers from .h to .m file of superclass and see which one is the matter of the problem. I'm sure that it's one of common headers used in both classes.

只需要执行所有可能的超类.h到.m文件的头文件,看看哪个是问题所在。我确信它是两个类中使用的一个通用头文件。

#6


0  

Like @Igor said, the order of imports matters:

如@Igor所说,进口顺序很重要:

I had to change

我不得不改变

#import <KeychainItemWrapper/KeychainItemWrapper.h>
#import <Foundation/Foundation.h>

to

#import <Foundation/Foundation.h>
#import <KeychainItemWrapper/KeychainItemWrapper.h>