Instance Variables in the Implementation File

March 5, 2012

Within Xcode, if you set the compiler option to Apple LLVM compiler 2.1 (or greater), you can move your instance variable declarations from the interface file to the implementation file.

For example, here is a traditional interface definition with instance variables declared:

@interface SandboxViewController : UIViewController <UITextFieldDelegate>
{
  UITextField *username;
  UIButton    *testButton;
  UILabel     *label;
}
@end

However, you can now move those same definitions to the implementation file:

@implementation SandboxViewController
{
  UITextField  *username;
  UIButton     *testButton;
  UILabel      *label;
}

The interface file would now look as follows:

@interface SandboxViewController : UIViewController <UITextFieldDelegate>
@end

This is a nice change, as you can now have the instance variables in the implementation, where they are actually used, and only expose what is public in the interface file.