“Cannot find interface declaration for NSObject”?

て烟熏妆下的殇ゞ 提交于 2019-11-28 23:35:42

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.

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:

#import "SomeClass.h"

In your .h files, whenever possible put:

@class SomeClass;

In your .h file, and put the #import statement in your .m file instead.

This can be caused by not including 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)

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

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

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"

The error got fixed when I added the import line.

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.

Also make sure you're not including an Objective-C file from a .cpp or .c file.

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.

A solution would be to use #ifdef __OBJC__ if you can get away with it.

Otherwise designate the .cpp file type as Objective-C++ Source in the file's properties panel (Top right window control => "Identity and type")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!