“无法找到NSObject的接口声明”?

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

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框架在objc中编写一个游戏。我一直在模拟器上工作直到这一点,一切都很顺利。但是当我在我的设备上切换到运行它时,我就会为那些应该是标准的东西产生各种各样的错误。“无法找到NSObject的接口声明”、“未知类型名称‘NSMutableArray'”等。我已经在每个类中都有#import,所以您会认为它不应该发生,对吗?我觉得这只是一两句话,需要在某个地方有所改变——但我一点也不知道。

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

6 个解决方案

#1


37  

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 #进口”酒吧。h”和酒吧。h #进口“foo。(有时它是一个由三个或多个头文件组成的链组成的链),它会导致错误的错误,就像你看到的那样。

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引用,并将#导入放到.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文件中。

#2


64  

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框架添加到项目中。(目标/构建阶段/链接库/链接库/——选择Add——添加UIKit.Framework)

#3


7  

Try this instead for Cocoa or iOS app, make sure to import "Foundation/Foundation.h" in your class where you are inheriting NSObject class.

试试这个吧,可以选择可可或iOS应用,确保导入“基础/基础”。在你的课堂上,你继承了NSObject类。

#4


4  

Try deleting the derived data for the project. You can do that through the organiser, under projects. You might have a corrupt precompiled header.

尝试删除项目的派生数据。你可以在项目中通过组织者来完成。您可能有一个损坏的预编译头文件。

#5


0  

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.

确保您不使用已经被占用的类名。当我给我的一个类“信号”命名时,我遇到了同样的问题,它已经是基础的一部分了。

#6


0  

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.

例如:PhotoManager。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++源代码(右上方窗口控件=>“标识和类型”)

#1


37  

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 #进口”酒吧。h”和酒吧。h #进口“foo。(有时它是一个由三个或多个头文件组成的链组成的链),它会导致错误的错误,就像你看到的那样。

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引用,并将#导入放到.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文件中。

#2


64  

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框架添加到项目中。(目标/构建阶段/链接库/链接库/——选择Add——添加UIKit.Framework)

#3


7  

Try this instead for Cocoa or iOS app, make sure to import "Foundation/Foundation.h" in your class where you are inheriting NSObject class.

试试这个吧,可以选择可可或iOS应用,确保导入“基础/基础”。在你的课堂上,你继承了NSObject类。

#4


4  

Try deleting the derived data for the project. You can do that through the organiser, under projects. You might have a corrupt precompiled header.

尝试删除项目的派生数据。你可以在项目中通过组织者来完成。您可能有一个损坏的预编译头文件。

#5


0  

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.

确保您不使用已经被占用的类名。当我给我的一个类“信号”命名时,我遇到了同样的问题,它已经是基础的一部分了。

#6


0  

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.

例如:PhotoManager。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++源代码(右上方窗口控件=>“标识和类型”)