views:

697

answers:

5

hopefully this is an easy one.

I'm coding in objective-c and i'm wondering if there are any tools/tricks (anything) that you use for this annoyance (see below).

Is there an easier way to declare variable/properties within the header and implementation files?

e.g., I'm not a big fan of typing this in the header:

NSString *commercial_name;

@property (nonatomic, retain) NSString *commercial_name

and then typing

@synthesize commercial_name

in the implementation

it's quite tedious when all 3 things are needed (or when I have to delete all 3) and I'm wondering if there's a plugin (or something) where you can simply say, I'm going to have a variable called foo of type bar and I want getter & setter methods for it. poof it's done.

TYVM!

A: 

there are autocompletion scripts (like typing 'log' and then pressing Cmd + . to autocomplete this to NSLog())

you could build one that would insert these three lines and you'd need to provide only the name and type for the property.

Eimantas
+4  A: 

On the iPhone, which uses the "modern runtime", you can ommit the ivar (field declaration). Simply declaring and synthesizing the property is enough. The ivar is created at runtime.

More info here (at the end of the page):

https://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html

Another discussion:

http://stackoverflow.com/questions/1007607/using-instance-variables-with-modern-runtime

Sadly, this mechanism doesn't work in the iPhone simulator, not even in Snow Leopard :-(

Philippe Leybaert
The new Objective-C runtime is 64-bit only, and it looks like the iPhone Simulator isn't yet 64-bit on Snow Leopard.
Brad Larson
+3  A: 

I'd check out Accessorizer: http://www.kevincallahan.org/software/accessorizer.html

Dave DeLong
+1 for the hint. didn't know the app
André Hoffmann
that's what i meant in my answer ,)
Eimantas
thanks for the suggestion!
A: 

Check out the following link, he has written an Apple Script which you can use in XCode and he has put the video as-well for us to see how to use it properly!

http://allancraig.net/blog/?p=315

itsaboutcode
Also check out some great tools for lazy Objective C programmers like me loxhttp://github.com/holtwick/xobjc/tree/master
itsaboutcode
thanks as well!
A: 

Accessorizer will write your property code for you (or your explicit accessors), and it will help provide you with the init, keypath, keyed-archiving, indexed accessors, accessors for unordered collections such as NSSet, copyWithZone, KVO, key-validation, singleton overrides, dealloc, setNilForKey, non-standard attribute persistence (Core Data), locking, headerdoc, convert method to selector, NSUndoManager methods and more including IBOutlet Detection, setting your UIKit views to nil in -viewDidUnload {...}, sorting your ivar declarations, property and synthesize statements, -dealloc {...} and -viewDidUnload {...} methods. Accessorizer also provides a caching mechanism for the custom table when working with properties.

Kevin