XCode 语法在预处理器 #if #else 的两种情况下都突出显示 [英] XCode syntax highlighting in both conditions of preprocessor #if #else

查看:51
本文介绍了XCode 语法在预处理器 #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 会自动检查我当前的目标是什么,并评估我认为不错的 #if/#else.问题是,它会关闭语法高亮、自动完成等任何不会被编译的条件.(例如,如果我当前的目标是模拟器,则实际实现中的代码将失去高亮显示)

XCode automatically checks to see what my current target is and evaluates the #if/#else which I guess is kind of nice. 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.

您还可以为模拟器实现库的虚拟版本.你会让它定义所有的接口,但一切都会返回失败甚至抛出异常.您可以在所有代码周围使用 #if TARGET_IPHONE_SIMULATOR,以便库最终完全为空以用于设备构建.然后,您将链接到该链接并使用第一种方法.

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. Then, you would link against that and use the first approach.

这篇关于XCode 语法在预处理器 #if #else 的两种情况下都突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆