Possible Duplicate:
@class vs. #import
I am new to Objective-c, I have seen an example look likes:
#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;
@end
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”?
Yes.
@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.
Why?
The reason why this was implemented (I think, anyway), is to prevent circular imports. Imagine a scenario where the following happens:
Class1.h
Class2.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.