为什么我的 Xcode 变得疯狂并且忘记了一些明确定义的类? [英] Why has my Xcode gone crazy and forgot some clearly defined classes?
问题描述
我在尝试构建时突然收到这些错误:
I'm suddenly getting these errors when I try to build:
它说 MenuViewController
是未知类型.但它在那里非常清楚,甚至是进口的.它在编译源中,我尝试了清理"、清理构建文件夹"、退出 Xcode、重新启动等.
It's saying MenuViewController
is an unknown type. But it's very clearly there and even imported. It's in Compile Sources, and I tried a "Clean", "Clean Build Folder", quit Xcode, reboot, etc.
在这种情况下发生了什么?我也可以命令单击 import 语句的类名,它会跳转到文件.当它是第三个属性中的 FavoritesViewController 时,它也说未知类型 MenuViewController...
What is going on in this case? I can also command click the import statement's class name and it jumps to the file. It's also saying unknown type MenuViewController when it's a FavoritesViewController in the third property...
推荐答案
说明
我已经看到当 .h 文件之间存在循环引用时会发生这种情况.#import
可防止文件被包含两次,但这有时意味着类未在您期望的位置声明.这是发生的事情:
I've seen this happen when there are circular references between .h files. #import
prevents the file from being included twice, but it sometimes means that classes aren't declared exactly where you expect. Here's what happens:
ClassA.m:
#import "ClassA.h"
...
ClassA.h
#import "ClassB.h"
@interface ClassA : ClassB
@end
ClassB.h
#import "ClassA.h"
@interface ClassB
@property (nonatomic) ClassA *classA;
@end
- ClassA.m 导入 ClassA.h.
- ClassA.h 在声明 ClassA 之前导入 ClassB.h.
- ClassB.h 导入了 ClassA.h,但它已经被导入了,所以这被忽略了.
- ClassB 尝试使用 ClassA,但尚未声明.
解决方案
我通常做的只是#import
.h 文件中的基类,前向声明类接口所需的任何类型,其余的#import
.m 文件中的 .h 文件:
What I usually do is to #import
just the base class in the .h file, forward declare any types needed by the class interface, and #import
the rest of the .h files in the .m file:
ClassA.m:
#import "ClassA.h"
#import "ClassB.h"
...
ClassA.h
#import "ClassB.h"
@interface ClassA : ClassB
@end
ClassB.h
@class ClassA;
@interface ClassB
@property (nonatomic) ClassA *classA;
@end
这篇关于为什么我的 Xcode 变得疯狂并且忘记了一些明确定义的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!