Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a BOOL property

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!

like image 898
Patrick Avatar asked Feb 01 '11 15:02

Patrick


People also ask

Is a Boolean type property?

The Boolean property data type stores a true or false value. When a Boolean property value is set, a null value is treated as false and any other value is treated as true. The attributes for a Boolean property include the true and false value.

What is the default value of BOOL Property C#?

The default value of the bool type is false .

How do you set a Boolean value in Objective C?

To set a BOOL, you need to wrap the number in a value object with [NSNumber numberWithBool:NO] .

Can BOOL be nil?

You can't. A BOOL is either YES or NO . There is no other state. The way around this would be to use an NSNumber ( [NSNumber numberWithBool:YES]; ), and then check to see if the NSNumber itself is nil .


2 Answers

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 
like image 116
BoltClock Avatar answered Oct 10 '22 22:10

BoltClock


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

like image 22
Hariprasad.J Avatar answered Oct 10 '22 23:10

Hariprasad.J