Skip to content

Instantly share code, notes, and snippets.

@mteece
Created July 28, 2014 19:38
Show Gist options
  • Save mteece/6b613b51031a0db77c24 to your computer and use it in GitHub Desktop.
Save mteece/6b613b51031a0db77c24 to your computer and use it in GitHub Desktop.
The past way and the the new and improved way of writing Objective-C classes, and properties with the modern Objective-C compiler.

The new way has several advantages. The primary advantage is that none of the implementation specific details about the class appear in the .h file. A client has no need to know what type of delegate (or delegates) the implementation needs. The client has no need to know what ivars I use. Now, if the implementation needs a new ivar or it needs to use a new protocol, the .h file doesn't change. This mean less code gets recompiled. It cleaner and much more efficient. It also makes for easier editing. When I'm editing the .m file and realize I need a new ivar, make the change in the same .m file I'm already editing. No need to swap back and forth.

Also note the implementation no longer needs an ivar or @synthesize for the property.

@interface ExampleClass : UIViewController
@property (nonatomic, assign) BOOL someBool;
// a few method declarations
@end
@interface ExampleClass () <UITextFieldDelegate>
@end
@implementation ExampleClass {
UITextField *_textField;
}
// the method implementations
@end
@interface ExampleClass : UIViewController <UITextFieldDelegate> {
UITextField *_textField;
BOOL _someBool;
}
@property (nonatomic, assign) BOOL someBool;
// a few method declarations
@end
@implementation ExampleClass
@synthesize someBool = _someBool;
// the method implementations
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment