Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

xcode - 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?

question from:https://stackoverflow.com/questions/5364506/how-to-assign-a-value-to-a-bool-pointer-in-objective-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...