views:

34

answers:

1

I have noticed in some of my older apps that in situations where I have added a custom View Controller I have not changed the View Controller class in the application delegate. For example, below I have created a CustomViewController class but have declared viewController as UIViewController.

@interface ApplicationAppDelegate: NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UIViewController *viewController;
}

My question is, both work, but for correctness should I be writing this as follows:

@class CustomViewController;
@interface ApplicationAppDelegate: NSObject <UIApplicationDelegate> {
    UIWindow *window;
    CustomViewController *viewController;
}

gary

+1  A: 

Yes, you should retrofit your existing member type definitions to be as specific as possible.

In the header file, you should always forward declare your instance objects.

This saves you from actually importing the class header, thereby saving a little overhead.

Forward declare as much as you can in header files, then #import in your implementation files.

Here's an example:

Header file:

@class CustomViewController;
@interface ApplicationAppDelegate: NSObject <UIApplicationDelegate> {
    UIWindow *window;
    CustomViewController *viewController;
}

Implementation file:

#import "CustomViewController.h";

@implementation ApplicationAppDelegate

...

@end
Jacob Relkin
Thank you, I think you just answered my next question too which I just posted about where to #inport. Much appreciated.
fuzzygoat
Can I just ask, in my first example above putting "UIViewController" is not correct, it should be "CustomViewController" ?
fuzzygoat
@fuzzygoat Yes. If the class is called `CustomViewController`, it should not be declared as its supertype.
Jacob Relkin
Thanks Jacob, much appreciated.
fuzzygoat