xcode 分配给 'BOOL 的不兼容整数到指针转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30386618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:11:05  来源:igfitidea点击:

Incompatible integer to pointer conversion assigning to 'BOOL

xcodeboolean

提问by Coder

I just inherent a new app from a previous programmer and keep on running into a "Incompatible integer to pointer conversion assigning to 'BOOL *' (aka 'bool *') from 'BOOL' (aka 'bool') ".

我只是从以前的程序员那里继承了一个新应用程序,并继续遇到“不兼容的整数到指针转换,从 'BOOL'(又名'bool')分配给'BOOL *'(又名'bool *')”

Code like such as

代码如

_backButtonPressed = YES;

_isEdited = YES;

come up with the same error. I tried *(_backButtonPressed) = YES; and *_backButtonPressed = Yes, but both scenarios crashes the program.

提出同样的错误。我试过 *(_backButtonPressed) = YES; 和 *_backButtonPressed = Yes,但这两种情况都会使程序崩溃。

Program still work if I leave it alone, but I wanted to keep the code clean. Any suggestion?

如果我不理会它,程序仍然可以工作,但我想保持代码干净。有什么建议吗?

回答by

My guess is that the two variables in question were declared incorrectly. For example, _backButtonPressed is probably declared as

我的猜测是有问题的两个变量声明不正确。例如,_backButtonPressed 可能被声明为

BOOL* _backButtonPressed;

but should be

但应该是

BOOL _backButtonPressed;

The crash occurs because _backButtonPress is not a pointer: if it was, there would be a statement like _backButtonPressed = (BOOL*)malloc(sizeof(BOOL)); somewhere. If this was not done before you try to assign to *(_backButtonPressed), then you will see a crash.

发生崩溃是因为 _backButtonPress 不是一个指针:如果是,就会有像 _backButtonPressed = (BOOL*)malloc(sizeof(BOOL)); 这样的语句。某处。如果在您尝试分配给 *(_backButtonPressed) 之前没有这样做,那么您将看到崩溃。

It is easy to make the mistake of declaring BOOL* instead of BOOL, because Cocoa objects are always declared with the *

声明 BOOL* 而不是 BOOL 很容易犯错误,因为 Cocoa 对象总是用 * 声明

I recommend that you fix the declarations. I am pretty sure everything will work if you do that.

我建议您修复声明。如果你这样做,我很确定一切都会奏效。