A better @synthesize
The biggest problem with Objective-C's
@synthesize directive for properties is how difficult it's to augment the synthesized code. You often need to add logic to a property setter, but while you're adding it, you're losing the probably correct implementation Apple's code creates for property flags like
At the moment, when you synthesize a
readwrite property called
foo you get a setter method called
setFoo. If you need to add logic around it, you can either store the value in a private property and add a public property with a different name or use a subclass. Both are a bit of a hassle. Usually I just end up writing my own method, including the logic for implementing the modifiers correctly.
In an ideal world the language would support something like around/before/after methods in CLOS, but those features are rare. There's a simple way
@synthesize could make things easier without requiring massive changes to the runtime. It could give you for both the getter and setter two methods. There would be the public methods they create in the current implementation, but there'd also be methods with names like
__synthesized_setProperty. The public methods would rely on the semi-private methods to actually implement all their logic. Then if you needed to add logic around the accessors, you could override the public methods and call the semi-private ones to get access to the synthesized accessor logic without jumping through hoops or risking getting the implementation wrong.