Thursday, February 5, 2009


In the days before Objective-C 2.0's properties, every Cocoa developer I knew used Kevin Callahan's great Accessorizer program, which would generate accessors and mutators for your instance variables, saving you tons of time.

Since I've been spending so much time on the iPhone lately and using Objective-C 2.0 properties, I must admit that I'd sorta forgotten about this fine program. After all, with properties, there's no longer a need to write accessors and mutators except in special circumstances. Well, Kevin hasn't been stagnating. he's recently updated Accessorizer with a bunch of new functionality. Most notably, it will now generate @property and @synthesize statements for you. The level of control you get over the way your code is generated is staggering. You can generate exactly what you need quickly, and Kevin's got a bunch of videos to show you to be even more productive by using Accessorizer from within Xcode. Accessorizer can be adjusted to suit the way you like to style your code, and it can even handle singletons and atomicity.

Having just finished writing a big class with lots of instance variables, I can say there is still very much a need for this program in my workflow. On one decent size application, this puppy will save you a lot of time. If you do any serious amount of Objective-C coding and aren't using Accessorizer, you should have your head examined.


Don Miller said...

In your book's code, and in Apple's sample code as well, it seems that the routine is to create properties from ALL instance variables, hence the need for a program like Accessorizer.

If there are dozens of instance variables but only several of those will ever be accessed by other objects (needing to be made into properties), why does everyone seem to make properties of every instance variable?

Jeff LaMarche said...


Well, there's more than one philosophy about this, just as there are with many aspects of coding. In our book, we did take our cue from Apple. We thought the best approach was to follow Apple's lead on such things, which is a habit Dave and I probably picked up from writing ADC articles.

But after all the Objective-C 2.0 code I've now written, I've come to agree with Apple's approach on this. I can't speak for Apple, but here are the advantages of using properties as I see them.

One advantage is that they can greatly help (especially newcomers) with memory management. Whenever you use your mutator method, the old object is released and the new one is retained. If you have no properties and no mutators, then you have to keep track of which methods need to retain objects and when, and which need to release - it can all get very messy, especially in larger classes. If you write methods generically (which is generally desirable), then it becomes even harder to make sure you're retaining and releasing at the right time without the use of the properties. Possible, yes, but not as easy.

Additionally, with using the accessor (or dot notation), you always have the ability to change the way the underlying data is stored or to make the property a derived or calculated property without having to change your code outside of the accessor (and maybe mutator) method. If you access instance variables directly all over the place, then such changes are much more involved.

That being said, I frequently use instance variables directly, usually (not always, but usually) these are raw datatypes like BOOL and NSTimeInterval rather than objects, since I do find it conceptually easier to manage retain counts using properties.

And yes, I do tend to err on the side of using properties when I'm not sure whether I need to. I definitely have created some properties that were never accessed by an external object and whose underlying storage was never changed.

It's like insurance. You buy it, even though you might not ever use it. It takes a few extra seconds to declare a property but, in a lot of situations, it can take a lot of additional time to refactor code in the future if you didn't.

That all being said, there's nothing "wrong" with only using properties for externally accessible objects. It's a design decision that some people would argue one way or the other. If using properties only for objects you know will be accessed externally seems natural to you, do it that way. I firmly believe that coders are most efficient when given the freedom to find out what works best for them.

Don Miller said...

Thanks for your take on my confusion.

Even with declared properties, I am still often confused about how others access and retrieve values in their code. Again, in Apple code (and maybe in your book), within a class, sometimes (or [self property]) is used for setting and getting, and many times not. Especially for setting, I would think the dot notation should ALWAYS be used for objects, and maybe it should ALWAYS be used for getting (in case there is something else going on).

I can see where some memory management issues go away with properties, but it still seems like a lot of effort for core data type (int, float, NSInteger, etc.). But I tried Accessorizer and it was already worth $15 to me. I can see using it at the end of a project to clean up memory leaks and such.

Jeff LaMarche said...


I definitely am more likely to use properties for objects than for raw datatypes - with raw datatypes, if I'm sure it won't be accessed by another object, I usually won't make a property out of it.



[self theProperty]

and (within the scope of the class)


are functionally identical, it's personal choice. With mutators,

self.theProperty = newProperty;

is the same as

[self setTheProperty:newProperty]

but that's NOT the same as

theProperty = newProperty.

In the first two, the property retains the object being assigned (assuming you used assign in your property), but the last one is just a direct assignment using an instance variable - no retain, which means you have a weak ref that could disappear.

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

yuya indou said...

hot mobile news
info ponsel
blogspot tips
mobile news today
info handphone
blogging tips
mobile phone news
info blackberry
blogspot tutorial
mobilephone reviews
harga ponsel baru
blogspot trick
iphone news
ponsel baru 2010
tips ngeblog
korean drama synopsis
korean drama
korean drama ost
korean drama movie
korean movie ost

JeansPilot said...

JeansPilot offers the chance to buy a large variety of men’s and women’s jeans clothing from the world famous Italian Brands.
Online jeans clothing store looks for original fashion clothing sales and clearances of worldwide known designers. We participate in fashion auctions to get the lowest possible price for Top quality Clothes, Shoes and Accessories.
Buy Jeans


I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up
Now it will be very cold in winter, in order to keep warm yourself, moncler company has provide jackets for you, so it is better to own moncler jackets yourselif as soon as possible. Eeveryone love fashion clothing, polo ralph lauren is very popular all over world, that is my dream to get ralph lauren.We know Ray Ban by America soldier, all of them wear ray ban sunglasses when they walk on the road, it is fashionable for you. Most of people like to wear jeans, it is very modern when wearing true religion jeans in the street.

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels