objective-ciosforward-declarationbuild-time

Importing header in objective c


In Objective-c when we using object of one class into another class by convention we should forward declare the class in .h file, i.e. @class classname;. And should import the header file in .m file, i.e. #import "header.h". But if we import the header file in .h then we don't have to import it again in .m file . So what is the reason behind this convention? Which is efficient way?


Solution

  • So what is the reason behind this convention?

    You should favor forward declarations (@class MONClass;) where possible because the compiler needs to know a typename is an objc class before it is used, and because an #import can drag in a ton of other headers (e.g. entire frameworks/libraries), seriously expanding and complicating your dependencies and increasing your build times.

    Which is efficient way?

    Forward declarations. Your builds, rebuilds, and indexing will be much faster if you do this correctly.