@property, dot syntax and a sprinkling of code
22/03/10 18:54
Until recently I've avoided dot syntax. I reluctantly adopted dot syntax due to the fact that Apple uses it extensively in iPhone Xcode templates. I had no desire to re-write the templates but I also wanted my code to be consistent. Therefore I bit the bullet and started using dot syntax.
I'm glad I made the change. With dot syntax I find it easier to see the difference between when the state of an object is being changed and when the object is being asked to perform an action.
(See In defense of Objective-C 2.0 Properties for a round up of the criticism and a defense of dot syntax).
Although I've only recently been using dot syntax I have always used the @property directive. Some times when setting (and occasionally getting) a property the object being called needs to do a little extra work, for example updating an internal cache. When using standard message syntax for setters this was easy to implement:
(See Objective-C 2.0 Accessors & Memory Management at Stay Hungry, Stay Foolish for a full description of this solution).
Unfortunately this solution doesn't work with dot syntax. With dot syntax...
is compiled into...
My solution is to use 2 property declarations. A private one that does the basic getting and setting and a public one which the extra code can be added to:
Admittedly this is a lot of boiler plate code which does slightly undermining the convenience of @property. However this approach allows a class to present a uniform interface for all of its properties.
I'm glad I made the change. With dot syntax I find it easier to see the difference between when the state of an object is being changed and when the object is being asked to perform an action.
(See In defense of Objective-C 2.0 Properties for a round up of the criticism and a defense of dot syntax).
Although I've only recently been using dot syntax I have always used the @property directive. Some times when setting (and occasionally getting) a property the object being called needs to do a little extra work, for example updating an internal cache. When using standard message syntax for setters this was easy to implement:
@interface ImageGallery : NSView{ ... NSArray* images;}@property(readwrite, retain, setter:setImagesSynth:) NSArray* images;-(void)setImages:(NSArray*)newImages;...@end@implementation ImageGallery...@synthesize images;-(void)setImages:(NSArray*)newImages{ [self setImagesSynth: newImages]; //do additional work here [self resizeCanvas];}@end(See Objective-C 2.0 Accessors & Memory Management at Stay Hungry, Stay Foolish for a full description of this solution).
Unfortunately this solution doesn't work with dot syntax. With dot syntax...
gallery.images = photos;is compiled into...
[gallery setImagesSynth: photos];My solution is to use 2 property declarations. A private one that does the basic getting and setting and a public one which the extra code can be added to:
@interface ImageGallery : NSView{ ... NSArray* imagesProperty;}@property(readwrite, retain) NSArray* imagesProperty;@property(readwrite, retain) NSArray* images;...@end@implementation ImageGallery...@synthesize imagesProperty;@dynamic images;-(NSArray*)images{ return self.imagesProperty;}-(void)setImages:(NSArray*)newImages{ self.setImagesProperty = newImages; //do additional work here [self resizeCanvas];}@endAdmittedly this is a lot of boiler plate code which does slightly undermining the convenience of @property. However this approach allows a class to present a uniform interface for all of its properties.