I have subclass of UIViewController called FullScreenViewController which has a property of type ImageScrollView which is subclassed UIScrollView object. The implementation and interface look as follows:
FullScreenViewController.h
#import <UIKit/UIKit.h>
@class ImageScrollView;
@interface FullScreenViewController : UIViewController
{
ImageScrollView *_scrollView;
}
@property(nonatomic, retain) ImageScrollView *scrollView;
@end
FullScreenViewController.m
#import "FullScreenViewController.h"
#import "ImageScrollView.h"
@implementation FullScreenViewController
@synthesize scrollView = _scrollView;
...
@end
now I subclass FullScreenViewController, and I try to access any properties from the ImageScrollView property and I keep getting the error message: “Property cannot be found”. When I add ImageScrollView.h to the subclass, it works, but I’m not understanding this. I’ve already added ImageScrollView.h in FullScreenViewController, why should I have to add it again?
UPDATE: Rather than using a forward class declaration, I’ve included ImageScrollView.h in FullScreenViewController.h. I’m a little confused as to why I’ve ever use a forward declaration versus just including the .h file?
Given the header:
a forward declaration
@class ImageScrollViewis all that’s needed. This tells the compiler that there is an objc class namedImageScrollView.Of course, with a forward declaration, the interface is not visible where you need to use it unless you also
#importImageScrollView where you use it.ImageScrollView‘s declaration is not visible to the subclassFullScreenViewControllerSubclass.ImageScrollView.his visible only where#imported.FullScreenViewController.mis not visible toFullScreenViewControllerSubclass.m. Therefore, you must write another#importinFullScreenViewControllerSubclass.mto useImageScrollViewthere.Use forwards for fast build times and sane, controlled dependency structures. This is a very time consuming problem to undo. And this problem gets much worse as the size of your programs and libraries increase:
Would you prefer a change to a header in a medium sized project to require recompilation of 50 sources with an average preprocessed input of 150,000 lines per translation, or would you favor that change to affect 6 sources with an average preprocessed input of 40,000 lines per translation? The difference here is that small change takes the incremental rebuild from seconds to minutes to complete, depending on how you have structured your dependencies and imports.