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

UFO

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.

why?

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
0

Comments

0 comments
Login to comment

Related

Why do I get an error for "__CrtGetFileInformationByHandleEx " when I try to compile

Why do I get an error from my API but only when I use some requests?

Why do I get a compile-time error when trying to return values from a mock?

Why do I get a compilation error when I try to have two methods with the same name and parameter type?

Why do I get a compile error when calling start method?

Why do I get an Undefined reference error when trying to compile?

Why do I get error "'cout' in namespace 'std' does not name a type" when I use "using cout = std::cout;"?

Why do i get this error when I try to perform some logical operation on dataframes?

Why do I get "functions that differ only by return type cant't be overloaded" error when nothing is really overloaded?

Why do I get the "Not a managed type" error?

Why do I get the error "expected type argument" when I annotate a type in a Rust function?

R lubridate: Why do I get a "unknown timezone" error?

Why do I get error on flurry when trying to compile to release on Android?

Why do I get the error "cannot find -lcurl" when trying to compile example code?

Why do I get a Compile error Argument not optional when all arguments are specified and non-null?

Why do I get a "no matching function" error when I open an fstream with the file name in a std::string?

Why do I get an error, when I instantiating prefab second time?

Why do I get an error when I add a border for every side, and then assign only one side?

Why do I get an Angular compile error in this simple component

Why do I get a type error in this program when I add an unrelated definition?

Why do I get “error: failed to push some refs” when pushing to Heroku?

How do I avoid an 'unbound variable' error when the query is unknown at compile but known when the function would be run?

Why do I get the error E0277: the size for values of type `[{integer}]` cannot be known at compilation time?

How do i assert, compile time, that the nth element of a tuple is an optional of some type?

Why do I get "Error Parsing Time" when I use the format string "%FT%T" with Time::Piece->strptime?

Why do I receive error TypeError: argument of type 'Name' is not iterable?

Why do I get an invalid column name error when using a temp table twice?

Why do I get this error when I run 'bower install'?

Why do I get an error when I use transduce?