mshaaban
mshaaban

Reputation: 211

when do we use @class in iPhone SDK

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

Upvotes: 1

Views: 652

Answers (1)

mk12
mk12

Reputation: 26374

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.

Upvotes: 3

Related Questions