What does @class do in Objective-C?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)