tags:

views:

58

answers:

2

can any one please explain me what does @class declaration do in objective & what are the cases in which we should use this declaration.

Thanks in advance

+10  A: 

It’s a forward declaration. It essentially tells the compiler that there’s a class of that name. I use it in the interface declarations:

@class Foo;
@interface Bar : NSObject {
    Foo *someFoo;
}
@end

Of course you could import the header for Foo instead:

#import "Foo.h"
@interface Bar : NSObject {
    Foo *someFoo;
}
@end

But if someFoo is not exposed to users of Bar, they would import an extra header file that’s of no use to them. With the @class declaration the users of Bar see no extra import, because Foo.h will be imported in the implementation file of Bar.

zoul
+2  A: 

it is called a forward declaration.

you use this directive to tell the compiler that there is an objc class with the name specified. your other options are to include the interface, or use id for variables or types.

this is helpful to minimize dependencies. i use them whenever i can to minimize dependencies, and significantly reduce build times.

it's the same as in c and c++:

struct mon_struct;
namespace MON { class mon_class; }
Justin
+1 for tipping that this should be preferred in order to reduce build times.
Victor Jalencas