Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a value to a BOOL pointer in Objective-C?

I'm having a bit of a confusion on how to assign a value to a BOOL pointer? Here's my code:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {     self.latitude.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];     self.longitude.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];      if (!initialBroadcast) {         initialBroadcast = YES; // Where I'm having troubles          [broadcastTimer fire];     }; } 

The compiler keeps telling me this: Incompatible integer to pointer conversion assigning to 'BOOL *' (aka 'signed char *') from 'BOOL' (aka 'signed char').

I'd appreciate a clarification on this since I am a nubski.


UPDATE

As many of you have pointed out, I am apparently abusing the declaration of a BOOL by using a pointer for it. To be honest, I don't know why I used it, but since I'm new to Objective-C it must have worked for what I was doing so it stuck.

Anyway, I have since changed the declaration to:

//  In .h @interface ... {     BOOL initialBroadcast; }  @property BOOL initialBroadcast;  //  In .m @synthesize initialBroadcast; 

So, am I on the right track now?

like image 565
Gup3rSuR4c Avatar asked Mar 19 '11 19:03

Gup3rSuR4c


People also ask

How do I set boolean value in Objective C?

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

What is the default value of BOOL in Objective C?

It is initialized to garbage. However, for a BOOL ivar, it will be initialized to NO , as the whole instance is filled with 0 on initialization. (Note: When ARC is enabled, local object pointers will always be have a default value nil , but local variables of non-object types like BOOL are still initialized to garbage.

Can BOOL be nil Objective C?

You can't. A BOOL is either YES or NO . There is no other state.

What is Boolean in Objective C?

Boolean, in Objective-C, is a hold over data type from the C language. Both in C, and hence in Objective-C, 0 (zero) is treated as “false” and 1 (one) as “true”. C has a Boolean data type, bool (note: the lowercase), which can take on the values of true and false.


1 Answers

You need to say

*initialBroadcast = YES; 

initialBroadcast is a pointer aka memory address. The * gives access to the value at the memory address that the pointer holds. So initialBroadcast is a memory address, but *initialBroadcast is a boolean or char.

like image 68
DavidMFrey Avatar answered Oct 14 '22 11:10

DavidMFrey