gpt4 book ai didi

ios - 为 BOOL 指针赋值时出现奇怪的警告问题

转载 作者:行者123 更新时间:2023-11-28 20:24:28 25 4
gpt4 key购买 nike

我正在 iOS6 中开发并使用 Xcode 4.5.2。

我有一个UIViewController,假设它的名字是aViewController。在此 UIViewController 中,我有一个 BOOL 指针 使用此代码声明为属性。实际上,这不是我实现的一段代码,所以我真的不知道为什么要使用 BOOL 指针

现在,当为这个 BOOL 指针 赋值时,我收到了这个著名的警告 Incompatible integer to pointer conversion assigning to 'BOOL *' (aka 'signed char *') from ' signed char' 这是完全可以理解的,因为在我的例子中,BOOL 指向一个 boolean 变量。

为了解决这个问题,我尝试了 this one .所以我的代码是这样的:

aViewController.aBoolPointer = YES;

然后,我试过了,

aViewController.(*aBoolPointer) = YES;

由于 Objective-c 是 C 的超集,我记得在 C 中 .* 符号等于 ->。上面的符号给出了一个错误。错误描述:Identifier expected

如果我把代码改成这样:

aViewController->aBoolPointer = YES ;

我收到此错误实例变量 aBoolPointer protected

这是一个奇怪的问题,如果我使用这段代码

aViewController.aBoolPointer = NO ;

然后,我没有收到任何警告。

所以我的问题是:

1-为什么我将 NO 值赋给 BOOL 指针 时没有收到警告?

2-如何正确地将YES值赋给BOOL指针

3-考虑到我解释的步骤,YESNO 之间有什么区别?

谢谢大家

最佳答案

1 Why i got no warning when i assign NO value to a BOOL pointer ?

因为NO等同于0,也就是NULL,所以相当于给NULL赋值指针,如:

BOOL *p = NULL;

2 How can i properly assign YES value to BOOL pointer ?

如果属性定义为:

@property(assign, nonatomic) BOOL *boolp;

然后:

BOOL b = YES;
obj.boolp = &b;

3 What is the difference between YES or NO by considering the steps i explained ?

我不明白这个问题。

关于ios - 为 BOOL 指针赋值时出现奇怪的警告问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14496539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com