Why do I get 'unknown type name DetailViewController' error, only some of the time when I compile?


This is something that happens some times but not all the time:

header of a class

#import "DetailViewController.h"

@interface MasterViewController : UIViewController

@property (nonatomic, weak) DetailViewController *detailViewController;

error on the property line: unknown type name DetailViewController

then I add this line after the import and the error vanishes.

@class DetailViewController;

This is something annoying that only happens once in a while. I have a lot of classes just like that, without the class line that work perfectly. Some require the class. Ah, and sometimes it happens on a class that was working fine for days, but from a point on, Xcode decide it doesn't know the class anymore and decides to annoy you.


Peter Foti

You never need to do both @class and import the header file, you should only do one or the other. @class tells the compiler that the class exists somewhere in your project. In header files you should really only ever be using @class to reduce circular dependencies and compile faster. Then once you've written @class in your header, you can import the actual .h file inside your implementation.

In this case it looks like you do in fact have a circular dependency which is the reason for the error.

Bottom line, in .h files use @class in .m import the full header.

Note: If you're subclassing X then @class won't work and you'll have to import the full header in your .h

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


TOP Ranking