The keyword of class and interface at header file in Objective-c

Possible Duplicate:
@class vs. #import

I am new to Objective-c, I have seen an example look likes:

  • How do third-party libraries work in Objective-C and Xcode?
  • Crash while getting my location on GoogleMaps on iPhone
  • Limit UITextField to one decimal point Swift
  • How to make a uiactionsheet dismiss when you tap outside eg above it?
  • App's App Store Link
  • Programmatically creating Views in IOS (how does it work)?
  • #import <UIKit/UIKit.h>
    @class MapKitSampleViewController;
    @interface MapKitSampleAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        MapKitSampleViewController *viewController;
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet MapKitSampleViewController *viewController;

    The above code is stored at “MapKitSampleAppDelegate.h” file, I want to ask what is the meaning of line 3 “@class MapKitSampleViewController;”? Can we change it to #import “MapKitSampleViewController.h”?

    2 Solutions Collect From Internet About “The keyword of class and interface at header file in Objective-c”

    I want to ask what is the meaning of line 3
    “MapKitSampleViewController”? Can we change it to #import


    @class keyword is a “forward declaration”. What you are telling the compiler is that this class is going to be used in this class, but the header import for it will be elsewhere.

    Most likely if you look in the .m file, you will find that the #import “MapKitSampleViewController.h” will be there.


    The reason why this was implemented (I think, anyway), is to prevent circular imports. Imagine a scenario where the following happens:


    #import Class2.h


    #import Class1.h

    Now, if I’m not wrong, what happens here is that during compilation, it will repeatedly import both header and bad things happen. The @class keyword is meant to prevent this from happening, because the import for those files will happen in the .m files, not in the .h files.

    BTW this is a duplicate of @class vs. #import

    So you will likely find more in-depth discourse on this topic at that question.

    Yes you can change it but this will increase the compilation time and will bring you no benefits.

    The “@class MapKitSampleViewController;” is a forward declaration see
    When using forward declaration you have to take care that you can use the forward declared class name only for type references.