I’m a beginner iOS/ObjectiveC coder and am trying to understand some details rather than just dumbly following the example code I see.
I think I get the gist of a forward declaration in a .h file: it tells the compiler that the specified class is a “player to be defined later” – and then the header is imported in the .m file.
What I don’t understand is why not just import the class header in the header where the class is referenced, rather than use a forward declaration? My understanding of #import is it won’t import the header more than once (as would #include – which necessitates the if_def stuff).
Do I have this all wrong?
One reason to use forward references is compiler speed. A header may be included in many other files and those files may not need the definitions included in your header file. Since included files are included by the preprocessor, having a lot of includes or large included files can greatly increase the lines of code that the compiler has to deal with.
You can see this yourself by using the preprocess command in Xcode to see the output of the preprocessor. By forward declaring classes in the header you are removing all of the code that would have been included by the header file.