简体   繁体   English

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

[英]Objective-C preprocessor directive for 'if not'

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. 但是,如果我只想做某事,如果不是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 here suggests not, but this seems like a very basic need. 这里的 Apple文档没有提示,但这似乎是一个非常基本的需求。

Basically I want to do the preprocessor equivalent of: 基本上我想做预处理器相当于:

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

you could try: 你可以尝试:

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

Are you trying to check if something is defined or not? 您是否尝试检查是否定义了某些内容? If yes, you can try: 如果是,您可以尝试:

#ifndef SOME_VARIABLE

or 要么

#if !defined(SOME_VARIABLE)

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. Apple文档( 如果 - C预处理器 )是正确的,这是C预处理器语句从一开始就是这样的方式。 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. 根据相同的文档,您所能做的就是制作一个表达式,该表达式计算为零或非零值并使用它。

Meccan's answers is correct as TARGET_IPHONE_SIMULATOR is defined as TRUE or FALSE depending 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 ( #if etc) are used for including or excluding things based on whether a symbol is defined or not. 通常,这些宏( #if等)用于根据是否定义符号来包括或排除事物。 For that use case the pre-processor has #ifdef and #ifndef which covers what has historically been accepted as the most important cases. 对于该用例,预处理器具有#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. 另外,鉴于这些陈述的主题只能是其他预处理器定义的符号(通过#define ),那么这种限制是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM