1

Possible Duplicate:
@class vs. #import
Cocoa: What's the difference between importing in the header and importing in the main file?

I just wanted to know what is the difference between importing a header file in our interface file and using @class in our interface file? And i have observed that if we import the header file in our interface file, we can directly create our instances of that class. Contrary to importing the header file, We have something like using @class in the interface file and importing the header in the implementation file? Can someone please throw some light on this?

Community
  • 1
  • 1
Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75

2 Answers2

9

@class simply references the class while import has strong binding to the the class that it includes the methods of the class and its implmentation.@class is also used to evade circularity or closed loop.The @class directive simply promises the compiler that a class exists, but tells it nothing more about the class while import has reference to each member of class so that at runtime compiler can know.

Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
1

Using forward declarations and importing headers you depend on only in the implementation file is the best practice. If you import in the header file you build up dependencies for the compiler. Say you import a.h in b.h and then b.h in c.h. Now when you change something in a.h the compiler will also have to recompile c.m because its dependencies have changed.

If you e.g. add a #define or #import in your precompiled header file (.pch) all your files will have to be recompiled.

Objective-C has guards against multiple imports (compare #include) so you will not have to check for multiple imports of the same header file.

soryu2
  • 144
  • 5