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

标签 objective-c xcode

我对如何为 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];
    };
}

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

由于我是 nubski,因此我希望能对此进行澄清。


更新

正如你们中的许多人所指出的,我显然滥用了 BOOL 的声明。通过使用它的指针。老实说,我不知道我为什么要使用它,但由于我是 Objective-C 的新手,所以它一定对我正在做的事情有用,所以它卡住了。

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

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

@property BOOL initialBroadcast;

//  In .m
@synthesize initialBroadcast;

那么,我现在走对了吗?

最佳答案

你需要说

*initialBroadcast = YES;

initialBroadcast 是一个指针,也就是内存地址。 * 可以访问指针所保存的内存地址处的值。所以initialBroadcast是一个内存地址,但是*initialBroadcast是一个 bool 值或者char。

关于objective-c - 如何在 Objective-C 中为 BOOL 指针赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5364506/

相关文章:

ios - 我可以异步UIImageJPEGRepresentation writeToFile吗?

xcode - 根据事件配置将文件复制到 bundle

iPhone xcode 4 CSSMERR_TP_CERT_NOT_VALID_YET

xcode - Xcode 中的 Storyboard 为 Controller 中的所有 View 生成不需要的 fixedFrame ="YES"

iphone - 使用 NSDateComponents 时如何不丢失 '0' 数字。(iphone)

objective-c - Apple 是否有办法仅在与当前队列不同时将 block 分派(dispatch)到队列?

ios - CAKeyframeAnimation动画开始没有第一帧(路径)

ios - 在 Xcode 中,有没有办法搜索调用特定方法的所有行?

objective-c - 无法撤消多个操作

JSON 请求并不总是返回相同的响应类型(对象和数组)