Apple recommends to declare a BOOL property this way:
@property (nonatomic, assign, getter=isWorking) BOOL working;
As I'm using Objective-C 2.0 properties and dot notation, I access this property using self.working
. I know that I could also use [self isWorking]
— but I don't have to.
So, as I'm using dot notation everywhere, why should I define an extra property? Would it be okay to simply write
@property (nonatomic, assign) BOOL working;
Or do I have any benefits writing getter=isWorking
in my case (usage of dot notation)?
Thanks!
This question is related to
objective-c
ios
properties
Apple recommends for stylistic purposes.If you write this code:
@property (nonatomic,assign) BOOL working;
Then you can not use [object isWorking].
It will show an error. But if you use below code means
@property (assign,getter=isWorking) BOOL working;
So you can use [object isWorking] .
There's no benefit to using properties with primitive types. @property
is used with heap allocated NSObjects
like NSString*
, NSNumber*
, UIButton*
, and etc, because memory managed accessors are created for free. When you create a BOOL
, the value is always allocated on the stack and does not require any special accessors to prevent memory leakage. isWorking
is simply the popular way of expressing the state of a boolean value.
In another OO language you would make a variable private bool working;
and two accessors: SetWorking
for the setter and IsWorking
for the accessor.
Source: Stackoverflow.com