Customising & issues with defaults of @Property
This is part of iOS Basics Series
In the previous post we looked at Properties under the hood.
We look at customisation and issues with default generation.
There could be following three issues ---
The names of getters / setters are designed to be with default naming convention
Not always one would want to have both setters and getters generated.
Not all getters / setters would be run in multithreaded programs.
We look at each case --
Customised Names to Getters / Setters --
Objective C provides attributes with @property directive so that one can have custom names for their getters / setters.
Here is how you achieve this -
@interface Car
@property (getter=isRunning) BOOL running;
@end
Now the method generated for getter would be as follows
-(BOOL) isRunning
{
return _running;
}
Preventing Setters from generating (readonly) --
Since Running is a state of car which can only be influenced by its internal operations, it would be wise to hide setter from other external objects.
We can achieve this by use of readonly attribute.
It is used as follows --
@interface Car
@property (getter=isRunning, readonly) BOOL running;
@end
Single Threaded or Nonatomic use cases
@property directive by default makes setters and getters atomic operations. This is very useful in multithreaded environment. But it comes at the cost of overhead to maintain synchronisation.
Not every application needs to deal in threads if for example you are making stop watch application you dont need threads. In such case one can conserve the overheads by using nonatomic attribute.
Here is how you use this --
@interface Car
@property (getter=isRunning, nonatomic) BOOL running;
@end
We look at memory management and Properties in next post


















