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
兩者有何區別
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。