简体   繁体   English

在预处理程序#if #else的两种情况下高亮显示XCode语法

[英]XCode syntax highlighting in both conditions of preprocessor #if #else

My app uses a lib that won't build and/or run on a simulator so I effectively stubbed out the references to that lib by surrounding references with preprocessor directives like so: 我的应用程序使用了一个不会在模拟器上构建和/或运行的库,因此我通过使用如下预处理器指令将引用周围的内容有效地排除了对该库的引用:

#if !(TARGET_IPHONE_SIMULATOR)
    //Do the real implementation
#else
    //Do a dummy implementation for testing

XCode automatically checks to see what my current target is and evaluates the #if/#else which I guess is kind of nice. XCode自动检查我当前的目标是什么,并评估我认为不错的#if /#else。 The problem is, it turns off syntax highlighting, autocomplete, etc for whichever condition is not going to be compiled. 问题是,无论要编译哪种条件,它都会关闭语法高亮显示,自动完成等功能。 (For example if my current target is the simulator, the code inside the real implementation loses its highlighting) (例如,如果我当前的目标是模拟器,则实际实现中的代码将失去突出显示的位置)

My bad solution is changing the target so that whichever implementation I want to edit gets 'activated'. 我不好的解决方案是更改目标,以便我要编辑的任何实现都被“激活”。 Is there a way to keep both of them highlighted at all times so I can seamlessly edit both? 有没有办法让它们始终保持突出显示,以便我可以无缝地编辑它们?

If both implementations will compile for either target, then you could do something like this: 如果两种实现都可以针对任一目标进行编译,则可以执行以下操作:

#if !(TARGET_IPHONE_SIMULATOR)
    const bool simulator = false;
#else
    const bool simulator = true;
#endif

    if (simulator)
    {
        //Do a dummy implementation for testing
    }
    else
    {
        //Do the real implementation
    }

Both branches will be compiled, although the optimizer should throw away the one which can never be used for a given target. 尽管优化器应丢弃永远不能用于给定目标的分支,但将编译两个分支。

If code using the library can't even be built when the target is the simulator, you can do something like this: 如果在目标是模拟器的情况下甚至无法使用库构建代码,则可以执行以下操作:

#if !TARGET_IPHONE_SIMULATOR
    if (true)
    {
        //Do the real implementation
    }
    else
#endif
    {
        //Do a dummy implementation for testing
    }

In this case, the real implementation still won't be syntax colored or support completion when targeting the simulator, but both branches will have those when targeting the device. 在这种情况下,以模拟器为目标时,真正的实现仍不会带有语法颜色或不支持完成功能,但是在以设备为目标时,两个分支都将具有这些颜色。

You could also implement a dummy version of the library for the simulator. 您也可以为模拟器实现虚拟版本的库。 You would have it define all of the interfaces, but everything would just return failure or even throw exceptions. 您将定义所有接口,但是所有接口都会返回失败甚至抛出异常。 You would use #if TARGET_IPHONE_SIMULATOR around all of the code so that the library ends up completely empty for device builds. 您将在所有代码周围使用#if TARGET_IPHONE_SIMULATOR ,以便该库最终对于设备构建完全为空。 Then, you would link against that and use the first approach. 然后,您将对此进行链接并使用第一种方法。

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

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