Custom getter and setter names

January 29th, 2011
#properties

When editing legacy code you sometimes come across the scenario where a getter and setter method has been created that performs the same job as a property would but a property wasn’t used. So you decide to tidy it and create a property for that instance variable. However the getter and setter don’t conform to the naming convention that Apple uses for properties, that's where the getter and setter attribute for properties comes in handle. These attributes let you create a custom name for the getter and setter while keeping the inbuilt functionality.

Getter:
@property (nonatomic, retain, getter=isMoving) bool moving;
Setter:
@property (nonatomic, retain, setter=newDirection) Direction *direction;
Both:
@property (nonatomic, retain, setter=newPrice, getter=currentPrice) double price;

What do you think? Let me know by getting in touch on Twitter - @wibosco

William Boles

William has been an iOS developer since 2010, after all these years he just hopes that the String API will stop changing now.