Why use @class directive instead of importing header file?


#1

Could someone explain the reasoning why the Class header file HypnosisView.h on page 118 is not directly imported into the HypnosisAppDelegate.h and uses the @class directive instead? And then import the header file in the implementation file.

I read somewhere on the web that it is better to avoid importing other header files into a header file and use the @class directive.
And then the header files are imported into the implementation file.

IMHO, if the header file is imported directly into a header file, you do not need to use the @class directive, and you do not need to import it in the implementation file. Could you please enlighten me on this?

Thank you.


#2

It is explained on page 118 in the book:
“Notice the @class directive after the import statement. This is a forward declaration for the class
HypnosisView. When you forward declare a class, you aren’t going as far as importing the header file;
you are just informing HypnosisterAppDelegate.h of the class HypnosisView so the compiler can
validate it. Forward declaring a class saves time when compiling – especially with large projects.


#3

Thank you for the reply.