XCode syntax highlighting in both conditions of preprocessor #if #else

南笙酒味 提交于 2019-12-04 15:44:51

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!