ios 'if not' 的 Objective-C 预处理器指令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10880510/
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-08-30 18:37:16  来源:igfitidea点击:

Objective-C preprocessor directive for 'if not'

iosobjective-ciphonec-preprocessorpreprocessor-directive

提问by Undistraction

I understand how to use a preprocessor directive like this:

我了解如何使用这样的预处理器指令:

#if SOME_VARIABLE
    // Do something
#else
    // Do something else
#endif

But what if I only want to do something IF NOT SOME_VARIABLE.

但是如果我只想做一些 IF NOT SOME_VARIABLE 怎么办。

Obviously I still could do this:

显然我仍然可以这样做:

#if SOME_VARIABLE

#else
    // Do something else
#endif

. . . leaving the if empty, But is there a way to do:

. . . 将 if 留空,但有没有办法做到:

#if not SOME_VARIABLE
   // Do something
#endif

Apple documentation heresuggests not, but this seems like a very basic need.

此处的Apple 文档不建议这样做,但这似乎是一个非常基本的需求。

Basically I want to do the preprocessor equivalent of:

基本上我想做的预处理器相当于:

if(!SOME_VARIABLE)(
{
   // Do Something
}

回答by CarlJ

you could try:

你可以试试:

#if !(SOME_VARIABLE)
   // Do something
#endif

回答by xuzhe

Are you trying to check if something is defined or not? If yes, you can try:

您是否要检查是否已定义某些内容?如果是,您可以尝试:

#ifndef SOME_VARIABLE

#ifndef SOME_VARIABLE

or

或者

#if !defined(SOME_VARIABLE)

#if !defined(SOME_VARIABLE)

回答by Peter M

The Apple documentation (If - The C Preprocessor) is correct and this is the way that C pre-processor statements have been since the dawn of time. As per that same documentation all you can do is craft an expression that evaluates to either zero or a non-zero value and use that.

Apple 文档(If - The C Preprocessor)是正确的,这就是 C 预处理器语句从一开始就采用的方式。根据相同的文档,您所能做的就是制作一个计算结果为零或非零值的表达式并使用它。

Meccan's answers is correct as TARGET_IPHONE_SIMULATORis defined as TRUEor FALSEdepending on the platform, so the expression will evaluate to either zero or a non-zero amount.

Meccan 的答案是正确的,因为平台TARGET_IPHONE_SIMULATOR定义TRUEFALSE取决于平台,因此表达式的计算结果为零或非零。

In general these macros (#ifetc) are used for including or excluding things based on whether a symbol is defined or not. For that use case the pre-processor has #ifdefand #ifndefwhich covers what has historically been accepted as the most important cases.

通常,这些宏(#if等)用于根据是否定义符号来包含或排除事物。对于该用例,预处理器具有#ifdef并且#ifndef涵盖了历史上被认为是最重要的情况。

Also given that the subject of these statements can only be other pre-processor defined symbols (via #define) then this limitation is reasonable.

还考虑到这些语句的主题只能是其他预处理器定义的符号(via #define),那么这种限制是合理的。