tags:

views:

36

answers:

2

Hello,

I'm confused about when do we use the @class keyword in code? and can we use the class header file instead ...

Any input would be appreciated,

Thanks,

Mohsen

+2  A: 

Say for example you have a custom NSView, MyView.h:

@interface MyView : NSView {
    // instance variables…
}
…
@end

And you need to use it in your application delegate. MyAppDelegate.H:

@class MyView;

@interface MyAppDelegate : NSObject <NSApplicationDelegate> {
   MyView *view;
}
…
@end

And then in MyAppDelegate.m:

#import "MyView.h"

@implementation MyAppDelegate
…
@end

There is no point in #importing MyView.h in MyAppDelegate.h because all you need is the name of the class. @class MyView is a forward declaration, saying that there is a class named MyView, so don't generate errors. It's sort of like a declaring a method prototype in C. Then in the MyAppDelegate.m, we need to import it for the methods and anything else declared in MyView.h.

Mk12
Thank you very much. Things are much clear now!!
mshaaban