112

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!

Patrick
  • 3,091
  • 4
  • 26
  • 29
  • 4
    Is this not a semantic based recommendation? so myCar.isWorking would be semantically more accurate than myCar.working – justcompile Feb 01 '11 at 15:16

3 Answers3

212

Apple simply recommends declaring an isX getter for stylistic purposes. It doesn't matter whether you customize the getter name or not, as long as you use the dot notation or message notation with the correct name. If you're going to use the dot notation it makes no difference, you still access it by the property name:

@property (nonatomic, assign) BOOL working;

[self setWorking:YES];         // Or self.working = YES;
BOOL working = [self working]; // Or = self.working;

Or

@property (nonatomic, assign, getter=isWorking) BOOL working;

[self setWorking:YES];           // Or self.working = YES;, same as above
BOOL working = [self isWorking]; // Or = self.working;, also same as above
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 4
    Surely its more to do with being key value coding compliant than just stylistic purposes? – Jasarien Feb 01 '11 at 16:30
  • 5
    It's a bit strange that Apple _recommends_ declaring those `isX` getters but Xcode is _not_ able to list them in the auto-completion popup. (In my example) `working` is listed there, but `isWorking` is not. So I don't see any benefits in declaring BOOL getters. I have to do more to be able to use them (declare the getter) but I get less (no auto-completion). – Patrick Feb 03 '11 at 08:35
4

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] .

Hariprasad.J
  • 307
  • 1
  • 12
-27

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.

Thomson Comer
  • 3,919
  • 3
  • 30
  • 32
  • 1
    You're not answering his question, namely what is the purpose of explicitly naming getter something else than the property (he's not asking if properties are a good idea). Also, properties allow for KVO and KVC, so the point you do make is misleading. – Martin Gjaldbaek Feb 01 '11 at 16:43
  • You're right that I overlooked the use of KVO and KVC with primitive properties. I think everyone in the thread is addressing his question regarding isWorking - it's a naming convention. – Thomson Comer Feb 01 '11 at 17:09
  • 26
    This is entirely incorrect; `@property` is very much intended to be used with primitive types and, for consistency's sake alone, has significant advantages. Furthermore, some primitive types (64 bit types on some 32 bit CPUs and 128 bit types on many 32 and 64 bit CPUs) are non-atomic on assignment; `@property`'s atomicity can be advantageous in those cases, too. – bbum Feb 01 '11 at 17:40
  • 1
    Interesting - but how the heck am I supposed to know that? :) Is this an implicity of the `atomic` and `nonatomic` attributes? – Thomson Comer Feb 01 '11 at 18:21