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?
To set a BOOL, you need to wrap the number in a value object with [NSNumber numberWithBool:NO] .
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.
You can't. A BOOL is either YES or NO . There is no other state.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With