开发者

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 apprecia开发者_如何学运维ted,

Thanks,

Mohsen


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜