-5

Why would we use the @class forward declaration in this code? What would happen if we comment out the @class PAWLoginViewController;? Thanks!

#import <UIKit/UIKit.h>

@class PAWLoginViewController;

@protocol PAWLoginViewControllerDelegate <NSObject>

- (void)loginViewControllerDidLogin:(PAWLoginViewController *)controller;

@end
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
csciXAV_12
  • 309
  • 1
  • 5
  • 13

2 Answers2

2

It's a forward declaration and lets the compiler know that the class exists without it having to see the full class declaration. It's designed to save compilation time.

EDIT Thanks to @nielsbot for reminding me that it's also good for avoiding circular dependencies when importing header files (i.e. A.h imports B.h which imports A.h).

If you actually want to use the class (like access members or derive from it), then the compiler will need to see the class declaration, which is normally accomplished using an #import pre-processor statement.

Droppy
  • 9,691
  • 1
  • 20
  • 27
  • I think it's more to save you from having to import entire header files and avoid header file interdependencies than it is to save compilation time, although it may have that effect. – nielsbot Dec 23 '14 at 08:12
  • @nielsbot What would be the benefit of avoiding the reading of lots of header files if not to save compilation time? However you are right, that it also avoids circular dependencies. – Droppy Dec 23 '14 at 08:13
  • Avoiding header file interdependencies, as I stated – nielsbot Dec 23 '14 at 08:14
  • Circular dependencies is what you meant though. – Droppy Dec 23 '14 at 08:14
  • Perhaps in most cases, yes. – nielsbot Dec 23 '14 at 08:16
1

In the simple way, If you comment out the @class then it will show the error as unknown type name. Because you are passing the class reference in the parameter. But you have not declared class name.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56