• Blog
  • Pictures
  • Projects
  • Cocoa

Current Post

  • You are viewing an archive page.
  • Click here to go to the top.

Previous Posts

  • Remembering and Rebuilding
    September 11, 2004
  • Optimism is the new punk
    September 9, 2004
  • Weekend in L.A.
    September 9, 2004
  • More Photoblogging
    August 31, 2004
  • Afternoon at the Beach
    August 23, 2004
  • Thanking Julia
    August 23, 2004
  • Fortune Cookie Wisdom
    August 14, 2004
  • Economic Anecdotes
    July 24, 2004
  • iChat Guitar Lesson
    July 20, 2004
  • Orcas Island Trip
    July 19, 2004

Archives

  • All Posts. Ever.

Contact

  • E-mail the author

Syndication

  • RSS
Accessorizer Updated
September 17, 2004 - permalink

Kevin Callahan has released a new version of Accessorizer — a Cocoa/Objective-C programmer's assistant that manages to be both a handy reference to the various accessor coding conventions and a productivity tool for doing real development.

You give it a list of instance variable declarations (either by selecing them in your source editor (e.g. Xcode) and invoking one of Accessorizer's convenient “Services” menu items, or by pasting or typing them into the Accessorizer window):

NSImage *image; NSPoint zoomCenter;

Accessorizer generates corresponding accessor method declarations:

- (NSImage *)image; - (void)setImage:(NSImage *)newImage; - (NSPoint)zoomCenter; - (void)setZoomCenter:(NSPoint)newZoomCenter;

and implementations:

- (NSImage *)image { return image; } - (void)setImage:(NSImage *)newImage { if (image != newImage) { id old = [self image]; image = [newImage retain]; [old release]; } } - (NSPoint)zoomCenter { return zoomCenter; } - (void)setZoomCenter:(NSPoint)newZoomCenter { zoomCenter = newZoomCenter; }

that you can drop right into your source code. Just build, and go!

Accessorizer is savvy about protocol specifiers and the standard non-object types, and offers a host of options for customizing the code it generates both appearance-wise and in functionally significant ways. Have your setters no-op redundant sets or not. Have your getters [[... retain] autorelease] if that's your cup of tea. Have both take locks if you're writing a model class that needs to be thread-safe.

Accessorizer will even implement keyed archiving for you:

- (void)encodeWithCoder:(NSCoder *)coder { if ([super encodeWithCoder:coder]) { [coder encodeObject:[self image] forKey:@"image"]; [coder encodePoint:[self zoomCenter] forKey:@"zoomCenter"]; } } - (id)initWithCoder:(NSCoder *)coder { if ([super initWithCoder:coder]) { [self setImage:[[coder decodeObjectForKey:@"image"] retain]]; [self setZoomCenter:[coder decodePointForKey:@"zoomCenter"]]; } return self; }

And for a multi-valued attribute...

NSMutableArray *sprites;

...Accessorizer will obligingly furnish KVO-compliant indexed accessors at the click of a button:

- (unsigned int)countOfSprites { return [[self sprites] count]; } - (id)objectInSpritesAtIndex:(unsigned int)index { id mySprites = [self sprites]; unsigned int spritesCount = [mySprites count]; if ( spritesCount == 0 || index > (spritesCount - 1) ) return nil; return [mySprites objectAtIndex:index]; } - (void)insertObject:(id)anObject inSpritesAtIndex:(unsigned int)index { id mySprites = [self sprites]; unsigned int spritesCount = [mySprites count]; if (index > spritesCount) return; if (anObject) [mySprites insertObject:anObject atIndex:index]; } - (void)removeObjectFromSpritesAtIndex:(unsigned int)index { id mySprites = [self sprites]; unsigned int spritesCount = [mySprites count]; if ( spritesCount == 0 || index > (spritesCount - 1) ) return; [mySprites removeObjectAtIndex:index]; } - (void)replaceObjectInSpritesAtIndex:(unsigned int)index withObject:(id)anObject { id mySprites = [self sprites]; unsigned int spritesCount = [mySprites count]; if ( spritesCount == 0 || index > (spritesCount - 1) ) return; [mySprites replaceObjectAtIndex:index withObject:anObject]; }

For a really productive development experience, keep the Accessorizer window minimized in the Dock, and invoke its functionality via the Services menu. This allows you to stay within Xcode (or whatever source editing environment you use) the entire time. Just select your ivar declarations, invoke an Accessorizer service via its key equivalent (e.g. “Implementation”, Cmd+2), move the insertion point to where you want the result, and hit paste (Cmd+V).

In all it's a very nifty app that can save you lots of time and typing. What little tedium remains with the advent of Cocoa Bindings, Accessorizer largely eliminates. Check it out! Then buy Kevin a well-deserved beer! :-)

Links
Accessorizer's home page
Accessorizer's VersionTracker page

 
© 2008 Troy N. Stephens
xhtml · css