views:

188

answers:

6

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?

+4  A: 

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.

Matthew Vines
Just be glad you're not writing getters and setters... Think of it as _more_ DRY than the alternatives!
Ben Gotow
Like all languages, some things in objective-c are a joy, while other things are a chore.
Matthew Vines
@ben actually if you're coding well, you don't have setters, getters or properties--but most people don't program very well (I should say you don't have setters and you have relatively few getters)
Bill K
@Ben, it's not that hard if you're using C#: `public string Foo { get; set; }` ...
Wim Hollebrandse
A: 

Objective-C is a very thin layer on top of C. At a low level, most of the "Objective" stuff compiles down to code pointers that are injected into the C compilation process.

They did add reference counting (the most basic garbage collection system) but because of it's nature, it's all done by hand, hence the "Release" calls.

The non-iPhone versions have added a real garbage collection system but those don't scale down as well because the freeing isn't as deterministic.

Keep your objects small and focused and the property syntax shouldn't be very invasive.

Bill K
A: 

Yes I do, but I hope daily someone will come up with a script or something for XCode to automate the thing. (I also prefer

self.foo = nil

in my deallocs)

IlDan
Apple recommends against using setters and getters in init and dealloc for good reason. For one thing, the object is incompletely defined during init/dealloc (especially if subclassed) and so calling other methods (including setters/getters) may cause incorrect behaviour. Consider the case of the setter being override or another object observing the property.
Peter N Lewis
Could you point me to that piece of documentation? It's hard to keep in sync with docs updates.
IlDan
A: 

You will be able to get around having to declare both the instance variable and the property once 32-bit computers are forgotten, as the 64-bit runtime can synthesize instance variables. But you can't be completely DRY in a language where things must be declared in a header file and implemented in another. The language requires duplication. The only way around in would be to create a metalanguage.

You can use scripts to eliminate the duplication of typing the same variable boilerplate over and over, but it will still be there in the code.

By the way, I don't think the @synthesize statement is duplicate information. Nothing else in your code indicates what accessors are used to get at the property.

Chuck
A: 

Since you mentioned that you're new to Objective-C, I want to make sure you're aware that you don't have to make properties for every instance variable in your classes, and in fact you probably shouldn't.

Properties represent an interface your class provides for other classes. This interface doesn't necessarily match up with the internal implementation of the class. In the cases where they are the same, the declared properties syntax makes it (relatively) simple to express that.

If your classes are exposing most of their internal state via properties, you may want to take a closer look at their design, and whether you might get a simpler design by moving responsibilities around.

Mark Bessey
A: 

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

@interface MoveMeViewController : UIViewController { }

@property(nonatomic, retain) IBOutlet UIImageView *imageView; @end

sam