Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bring DRY (don't repeat yourself) to Objective-C

I'm coming from Ruby to Objective-C and I keep doing:

NSObject *foo;

@property (nonatomic,retain) NSObject *foo;

in the .h file, and then in .m file:

@synthesize foo;

at the top and

[foo release]

in dealloc.

It's 4 steps to add foo! Do seasoned Objective-C programmers do all four steps manually each and every time they want to add a new instance variable to a class? Am I missing a way to make this DRY?

like image 392
Andrew Arrow Avatar asked Jul 31 '09 21:07

Andrew Arrow


2 Answers

This is a common concern in C++ as well (doubling up of declarations, though it is admittedly a little different). The short answer is that it is how the language is constructed. It's doesn't really go against DRY since each statement is unique and has its own purpose. However it is admittedly very verbose by today's standards.

like image 108
Matthew Vines Avatar answered Sep 23 '22 20:09

Matthew Vines


This page http://pragprog.com/magazines/2010-07/not-quite-new-in-ios- claims, that you can drop the variable declaration, bringing it down to 2 repeats only :)

#import <UIKit/UIKit.h>
@interface MoveMeViewController : UIViewController {
}

@property(nonatomic, retain) IBOutlet UIImageView *imageView;
@end
like image 30
sam Avatar answered Sep 23 '22 20:09

sam