07 May 2010

#import vs. @class

I had a question today at the end of the lecture, about the distinction between #import and @class ("forward" class declaration) Here's a brief recipe, based on the textbook (top of p. 69):
  • In implementation (.m) files, use #import to access declarations for classes you create; e.g.,
    #import "CityDelegate.h"
  • In header (.h / interface) files, use #import to access declarations for Cocoa classes: e.g.,
    #import 〈UIKit/UIKit.h〉
  • In header (.h / interface) files, use @class to access declarations for other classes you create; e.g.,
    @class RootController
In principle, you should be able use #import to include any code anywhere you want. As the author says, however, @class is recommended by Apple for including your own class declarations in your own interface files, because "it avoids potential problems that may come with importing files that import still other files."

No comments:

Post a Comment