如何在Objective-C中为BOOL指针赋值?

Gup*_*R4c 37 xcode objective-c

关于如何为BOOL指针赋值,我有点困惑?这是我的代码:

- (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];
    };
}
Run Code Online (Sandbox Code Playgroud)

编译器一直告诉我:Incompatible integer to pointer conversion assigning to 'BOOL *' (aka 'signed char *') from 'BOOL' (aka 'signed char').

因为我是一名nubski,所以我对此表示赞赏.


UPDATE

正如你们许多人所指出的那样,我显然是BOOL通过使用指针来滥用a的声明.说实话,我不知道为什么我使用它,但是因为我是Objective-C的新手,所以它必须适用于我正在做的事情,所以它卡住了.

无论如何,我已经将声明改为:

//  In .h
@interface ... {
    BOOL initialBroadcast;
}

@property BOOL initialBroadcast;

//  In .m
@synthesize initialBroadcast;
Run Code Online (Sandbox Code Playgroud)

那么,我现在正走在正确的轨道上吗?

Dav*_*rey 60

你需要说

*initialBroadcast = YES;
Run Code Online (Sandbox Code Playgroud)

initialBroadcast是一个指针,也就是内存地址.*允许访问指针所在的内存地址的值.所以initialBroadcast是一个内存地址,但*initialBroadcast是一个布尔值或字符.


bbu*_*bum 59

问题不在于赋值,更有可能是您声明了实例变量BOOL *initialBroadcast;.

没有理由将实例变量声明为指针(至少除非你确实需要一个BOOL的C数组).从声明中删除*.

同样,这将修复您当前不正确的if()测试.实际上,它正在检查是否设置了指针,而不是值.


Mah*_*esh 8

改变 -

initialBroadcast = YES;
Run Code Online (Sandbox Code Playgroud)

(*initialBroadcast) = YES;
Run Code Online (Sandbox Code Playgroud)

因为,您要为指针指向的位置赋值(假设它已初始化),initialBroadCast应首先取消引用.