ios “找不到 NSObject 的接口声明”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8994911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Cannot find interface declaration for NSObject"?
提问by Dylan Beck
So I've done some research into this issue, but I haven't found anything similar just yet...
所以我对这个问题做了一些研究,但我还没有发现任何类似的东西......
So I'm coding a game in Obj-C using Xcode and Sparrow Framework. I've been working on the simulator up until this point, and it's all been going fine. But when I switch to running it on my device, I get all sorts of errors for things that should be standard, e.g. "Cannot find interface declaration for NSObject", "Unknown type name 'NSMutableArray'" etc. I've already got #import in each class, so you would think that it shouldn't happen, right? I get the feeling that it's just a line or two that needs changing somewhere - but I haven't got a clue what or where.
所以我正在使用 Xcode 和 Sparrow Framework 在 Obj-C 中编写游戏。到目前为止,我一直在模拟器上工作,一切顺利。但是当我切换到在我的设备上运行它时,我会收到各种应该是标准的错误,例如“找不到 NSObject 的接口声明”、“未知类型名称‘NSMutableArray’”等。我已经有了 #在每个类中导入,所以你会认为它不应该发生,对吧?我觉得它只是一两行需要在某处更改 - 但我不知道是什么或在哪里。
If anyone has any advice, it would be very much appreciated. :)
如果有人有任何建议,将不胜感激。:)
EDIT: Here's a screenshot of one of the .h files that give errors - it seems to only be in some of the .h files that I've created. http://i.imgur.com/EuQh4.png
编辑:这是给出错误的 .h 文件之一的屏幕截图 - 它似乎只出现在我创建的一些 .h 文件中。http://i.imgur.com/EuQh4.png
回答by Danoli3
This can be caused by not including UIKit.
这可能是由于不包含 UIKit 造成的。
Add this to your header:
将此添加到您的标题:
#include <UIKit/UIKit.h>
Also make sure to add the UIKit Framework to your project. (Targets/Build Phases/Link Binary With Libraries/ -- Select Add --- Add UIKit.Framework)
还要确保将 UIKit 框架添加到您的项目中。(Targets/Build Phases/Link Binary With Libraries/ -- 选择 Add --- Add UIKit.Framework)
回答by Nick Lockwood
It sounds like you may have a circular reference in one of your header files.
听起来您的头文件之一中可能有循环引用。
This can happen when foo.h #imports "bar.h" and bar.h #imports "foo.h" (or sometimes its a chain of three or more header files importing each other in a circle) and it leads to spurious errors like the one you're seeing.
这可能发生在 foo.h #imports "bar.h" 和 bar.h #imports "foo.h"(或者有时它是一个由三个或更多头文件组成的链,在一个圆圈中相互导入)并导致虚假错误就像你看到的那个。
The solution is to try to avoid importing headers in your .h files, and instead use @class references for external classes in the .h files and put the #imports in the .m files instead. So instead of writing:
解决方案是尽量避免在 .h 文件中导入标头,而是对 .h 文件中的外部类使用 @class 引用,而将 #imports 放在 .m 文件中。所以而不是写:
#import "SomeClass.h"
In your .h files, whenever possible put:
在您的 .h 文件中,尽可能输入:
@class SomeClass;
In your .h file, and put the #import statement in your .m file instead.
在您的 .h 文件中,并将 #import 语句放在您的 .m 文件中。
回答by Reetesh Kumar
Try this instead for Cocoa or iOS app, make sure to import "Foundation/Foundation.h" in your class where you are inheriting NSObject class.
对于 Cocoa 或 iOS 应用程序试试这个,确保在继承 NSObject 类的类中导入“Foundation/Foundation.h”。
回答by joerick
Try deleting the derived data for the project. You can do that through the organiser, under projects. You might have a corrupt precompiled header.
尝试删除项目的派生数据。您可以通过组织者在项目下完成此操作。您可能有一个损坏的预编译头。
回答by suryashekhar
I faced a similar error, resolved it when I noticed I had done something remarkably stupid.
我遇到了类似的错误,当我注意到我做了一些非常愚蠢的事情时解决了它。
In my foo.m
file, I had forgotten to #import "foo.h"
在我的foo.m
档案中,我忘记了#import "foo.h"
The error got fixed when I added the import line.
当我添加导入行时,错误得到了修复。
回答by Luc Bloom
Also make sure you're not including an Objective-C file from a .cpp
or .c
file.
还要确保您没有从.cpp
或.c
文件中包含 Objective-C文件。
Example: a PhotoManager.mm
might include the header file from the Objective-C pair PhotoObject.h
/PhotoObject.mm
.
Then, if MyAwesomeCppFile.cpp
includes PhotoManager.h
, suddenly PhotoObject.h
doesn't know the basic Objective-C classes and keywords.
示例: aPhotoManager.mm
可能包含来自 Objective-C 对PhotoObject.h
/的头文件PhotoObject.mm
。然后,如果MyAwesomeCppFile.cpp
包含PhotoManager.h
,突然PhotoObject.h
不知道基本的Objective-C 类和关键字。
A solution would be to use #ifdef __OBJC__
if you can get away with it.
一个解决方案是使用,#ifdef __OBJC__
如果你能逃脱它。
Otherwise designate the .cpp
file type as Objective-C++ Source
in the file's properties panel (Top right window control => "Identity and type")
否则在文件的属性面板中指定.cpp
文件类型Objective-C++ Source
(右上角的窗口控件 =>“身份和类型”)
回答by d4n13l
Make sure that you don't use a Class Name that is already taken. I've had the same problem when i named one of my Classes "Signal", which is already part of Foundation.
确保您没有使用已经被占用的类名。当我将我的一个类命名为“信号”时,我遇到了同样的问题,它已经是 Foundation 的一部分。