ios 条件编译, Active Compilation Conditions和Preprocessor Macros的区别

【已解决】Xcode中Active Compilation Conditions和Preprocessor Macros的区别

条件编译作用:

在实际开发中我们常常需要区分不同的环境,此处以最简单的开发与生产环境为例,每次打包通过修改代码区分不同的环境过于繁琐,并且如果需要修改的地方过多,忘改了某一处的话就会造成环境不统一,不仅给开发人员增加负担,对测试同事也是麻烦的一件事。因此,通过预处理宏能很好的解决我们这一问题。

但实际上我们仅仅开发与生产环境是不够的,往往还需要测试,预上线环境。Xcode使用Build configuration 配置多种项目环境

此外,通过预处理宏也能用于区分不同的target版本。

在C 系语言中,我们可以通过预处理宏定义一些参数,使用#if或者#ifdef编译条件分支来控制哪些代码需要编译,而哪些代码不需要。但是在swift中没有宏定义的概念,虽然不能使用 #ifdef 的方法来检查某个符号是否经过宏定义,但是可以支持“#if/#else/#endif”语句。


以上<条件编译作用>转自:
作者:oneday527
链接:https://www.jianshu.com/p/9a93e614a98e
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

折腾:

【已解决】Xcode项目条件编译出错:Use of unresolved identifier

期间,需要

搞清楚,Xcode中的,都是用于 条件编译时,所需要定义变量时,有两个:

Active Compilation Conditions和Preprocessor Macros

扫描二维码关注公众号,回复: 3657316 查看本文章

两者有何区别

Active Compilation Conditions vs Preprocessor Macros

Super Preprocessor Directives with Xcode 8 – Derrick Ho – Medium

原来是:

对于OBJC,用:Preprocessor Macros

对于SWIFT,用:Active Compilation Conditions

xcode – #ifdef replacement in the Swift language – Stack Overflow

SWIFT_ACTIVE_COMPILATION_CONDITIONS

“Active Compilation Conditions” is a new build setting for passing conditional compilation flags to the Swift compiler.”

是Xcode8中新增的。

Using Swift with Cocoa and Objective-C (Swift 4): Interacting with C APIs

“Preprocessor Directives

The Swift compiler does not include a preprocessor. Instead, it takes advantage of compile-time attributes, conditional compilation blocks, and language features to accomplish the same functionality. For this reason, preprocessor directives are not imported in Swift.”

swift Active Compilation Conditions

Xcode 8: New build settings and analyzer improvements – miqu.me

之前swift中定义自定义变量时,都是放在OTHER_SWIFT_FLAGS中,现在都可以改用SWIFT_ACTIVE_COMPILATION_CONDITIONS的Active Compilation Conditions了。

xcode – Any way to do true conditional compilation in Swift 3? – Stack Overflow

“Active Compilation Conditions is a new build setting for passing conditional compilation flags to the Swift compiler. Each element of the value of this setting passes to swiftc prefixed with -D, in the same way that elements of Preprocessor Macros pass to clang with the same prefix. (22457329)”

【总结】

  • 在之前OC时代(编译器是clang),条件编译所用到的变量定义,都是通过:GCC_PREPROCESSOR_DEFINITIONS的Preprocessor Macros去定义的;
  • 后来swift中(编译器是swiftc),最开始时是用:OTHER_SWIFT_FLAGS的Other Swift Flags中定义的(加上-DXXX)
  • Xcode 8中又更新为,换用SWIFT_ACTIVE_COMPILATION_CONDITIONS的Active Compilation Conditions去定义变量XXX即可(不需要加-D)。

此处,Xcode9中,当前代码是swift,用的编译系统也是swift,所以变量定义是用Active Compilation Conditions,而不是Preprocessor Macros。

猜你喜欢

转载自blog.csdn.net/LeeCSDN77/article/details/82901218