"Cannot find interface declaration for NSObject"?
Asked Answered
P

7

21

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.

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. https://i.stack.imgur.com/H04mE.png

Peregrine answered 24/1, 2012 at 22:4 Comment(10)
Sounds like Foundation isn't imported somehow. Try adding #import <Foundation/Foundation.h> to your prefix.pch to see if it clears up the warnings.Protuberance
Just checked out Application_Prefix.pch - it's already in there, along with <UIKit/UIKit.h> and "Sparrow.h" O.oPeregrine
Is Foundation listed in your frameworks folder? Are you linking to it in your target settings?Protuberance
Yes to the first question, and I don't have a clue about the second one XD Sorry, still a bit green to Obj-C/Xcode.Peregrine
Ok, just checked the Linked Frameworks and Libraries - I hope that's the one you were talking about - and I see Foundation.framework in there.Peregrine
I think there's a bit more going on here than you let on in your question. Please take a screenshot of your Xcode window with the source file (and the errors that appear with it) and edit your question to add this screenshot. My thinking is that it's either a typo in your .h or .m file.Transpose
Updated - feel free to check it out :)Peregrine
So I tried creating a new project, and shifting all my files/resources over to it. Gave it a run on the iPod, and it worked fine... until I changed the Product Name in the Build Settings. Now it's giving me the exact same issues...Peregrine
Can you command click on the #import line to go to Foundation/Foundation.h?Thwack
Sure thing - i.imgur.com/PoNDq.pngPeregrine
B
49

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.

Bananas answered 25/1, 2012 at 1:50 Comment(3)
Hi, I gave this a shot, and it seems to do the trick for most of these errors, thank you! It's odd though, I checked for circular references and there didn't seem to be any that I could see... Also, there still seem to be a few left over (we've gone down from 20+ errors to 5), but I'll keep at it. Thanks again :)Peregrine
Still, I don't get it. When I use import statement (instead of include), multiple inclusion should be handled and prevented, that's why I don't really see a possible reason for this behavior.Dolley
@Dolley the import statement prevents the same file being imported more than once, but it doesn't solve circular inclusions: Suppose that file A imports file B and file B imports file A; file A hasn't been fully imported by the time file B tries to import it again, and if file B needs to refer to stuff declared in file A after the import B statement, then it can't because it hasn't parsed that part yet.Bananas
T
71

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)

Trackandfield answered 24/11, 2014 at 3:2 Comment(3)
It seems new Xcode does not include UIKit framework by default.Thorstein
Yeah this is very important for Xcode 6 and iOS 8+ SDK.Trackandfield
This is actually because Xcode projects used to include a prefix.pch file containing #include <UIKit/UIKit.h>, but now they don't.Bananas
B
49

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.

Bananas answered 25/1, 2012 at 1:50 Comment(3)
Hi, I gave this a shot, and it seems to do the trick for most of these errors, thank you! It's odd though, I checked for circular references and there didn't seem to be any that I could see... Also, there still seem to be a few left over (we've gone down from 20+ errors to 5), but I'll keep at it. Thanks again :)Peregrine
Still, I don't get it. When I use import statement (instead of include), multiple inclusion should be handled and prevented, that's why I don't really see a possible reason for this behavior.Dolley
@Dolley the import statement prevents the same file being imported more than once, but it doesn't solve circular inclusions: Suppose that file A imports file B and file B imports file A; file A hasn't been fully imported by the time file B tries to import it again, and if file B needs to refer to stuff declared in file A after the import B statement, then it can't because it hasn't parsed that part yet.Bananas
C
10

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

Cycloparaffin answered 12/8, 2015 at 21:19 Comment(0)
T
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.

Thwack answered 24/1, 2012 at 23:29 Comment(4)
Just tried it, although it won't let me delete any derived data. The button's greyed out, so I'm guessing there's none there in the first place?Peregrine
Ok, that might not be it then, but just in case, go to /Users/<username>/Library/Developer/Xcode/DerivedData and find the project folder, and delete it. It's safe to delete all the folders in here if you're not sure which one is the right one, Xcode will just have to reindex your source files.Thwack
Went to go check it out, but there was no data in there to begin with. It's a bit of a mystery!Peregrine
It may not have worked for OP, but it worked for me! Thank you!Tasset
N
2

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.

Neilla answered 4/5, 2018 at 6:16 Comment(0)
B
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.

Bentwood answered 16/8, 2012 at 20:51 Comment(0)
J
0

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")

Joseph answered 12/12, 2017 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.