Objective-C 101: Difference between #import and @class

A 101 note but just in case this bit of information has eluded some, I thought I’d explain the difference between importing a class and referring to a class with forward referral. Working with an interface class, you get all the goodness of the inherited classes, right from the top with NSObject and down the hierarchy. If you mention a class that doesnt follow that chain, you will need to import it explicitly.

When working with forward referral, such as referring to @class User, you tell your interface header file that they are class names only, rather than import their interface files. This would be useful if you are merely mentioning static typed instance variables, such as :

- (BOOL)isValidUser:(User *)aUser;


Because you are only just mentioning the class name User, you are not dependent on any other properties of the class interface, you use @class. What you get is a minimalisation of code seen by the compiler and linker, as well as resolve circular conflicts of one header importing another which imports the first one, and so forth. This is Apple’s explanation :

The Role of the Interface

The purpose of the interface file is to declare the new class to other source modules (and to other programmers). It contains information they need to work with the class (programmers might also appreciate a little documentation).

•The interface file tells users how the class is connected into the inheritance hierarchy and what other classes—inherited or simply referred to somewhere in the class—are needed.

•Through its list of method declarations, the interface file lets other modules know what messages can be sent to the class object and instances of the class. Every method that can be used outside the class definition is declared in the interface file; methods that are internal to the class implementation can be omitted.