ruby_newbie
ruby_newbie

Reputation: 129

Purpose of @class in Objective-C

I have the following code

#import <UIKit/UIKit.h>
#import "SecondLevelViewController.h"
@class DisclosureButtonController;

@interface DisclosureButtonController : SecondLevelViewController {
    NSArray *list;
    DisclosureButtonController *childController;
}

@property (nonatomic, retain) NSArray *list;

@end

I can`t get what

@class DisclosureButtonController;

means.

Can anyone explain to me ?

Upvotes: 0

Views: 175

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

It simply tells the compiler that DisclosureButtonController is a Class that is defined elsewhere.

If you remove it you should get an error at DisclosureButtonController *childController; because the compiler doesn't know what you want him to do at this line.

From Apple Doc Defining a class

The @class directive minimizes the amount of code seen by the compiler and linker, and is therefore the simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that may come with importing files that import still other files. For example, if one class declares a statically typed instance variable of another class, and their two interface files import each other, neither class may compile correctly.


EDIT: I just saw that the @class directive is superfluous there, because you are declaring this class at the next line. Maybe there was a @protocol that used the class in between @class and @interface. But in your special case you could remove it without problems. It's redundant.

Upvotes: 3

Related Questions