如何在Objective-C中为BOOL指针分配值? [英] How to assign a value to a BOOL pointer in Objective-C?

查看:132
本文介绍了如何在Objective-C中为BOOL指针分配值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何为BOOL指针分配值有些困惑?这是我的代码:

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];
    };
}

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

我很乐意澄清一下,因为我是个nubski.

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

更新

正如许多人所指出的,我显然在通过使用指针来滥用BOOL的声明.老实说,我不知道为什么要使用它,但是由于我是Objective-C的新手,所以它一定能按我的方式工作,因此卡住了.

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?

推荐答案

您需要说

*initialBroadcast = YES;

initialBroadcast是一个指针,又名内存地址. *可以访问指针保存的内存地址中的值.因此,initialBroadcast是一个内存地址,但是* initialBroadcast是一个布尔值或char.

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.

这篇关于如何在Objective-C中为BOOL指针分配值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆