Xcode: stop lines of code / functions from compiling

和自甴很熟 提交于 2019-12-11 19:08:52

问题


I have common files in several projects and I need to stop from compiling some lines and functions for project A, meanwhile it should be compiled for project B.

I know that I can use preprocessor. But it's not convenient for me. Is there any way to stop lines of code from compiling with condition like below?

#if PhotosModuleSettings.type == .documents
... do not commpile
#endif

回答1:


What's not convenient about using the preprocessor? You can specify the preprocessor macros in build settings of each target, or you can use .xcconfig files to specify them.

There's another simple way to do it, however. Separate the lines and functions that you want to conditionally compile into separate files. Maybe by using Swift extensions or subclassing or just separate global functions, etc..whatever. Then just choose which target(s) and/or project(s) you want those files added as membership.

Depending on your desire to refactor your code to make such a file separation, the preprocessor macros may be the better way to go, though.




回答2:


You will need to make use of pre processor macros.

Add a configuration for your project, and use that in the pre processor macros.

You can set the value for these configuration in the pre processor macros section for your targets based on your build configuration.

Here is a detailed blog related to the same concept



来源:https://stackoverflow.com/questions/55133043/xcode-stop-lines-of-code-functions-from-compiling

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