#ifdef replacement in the Swift language

前端 未结 17 1724
名媛妹妹
名媛妹妹 2020-11-22 10:35

In C/C++/Objective C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors.

         


        
17条回答
  •  心在旅途
    2020-11-22 11:29

    A major change of ifdef replacement came up with Xcode 8. i.e use of Active Compilation Conditions.

    Refer to Building and Linking in Xcode 8 Release note.

    New build settings

    New setting: SWIFT_ACTIVE_COMPILATION_CONDITIONS

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

    Previously, we had to declare your conditional compilation flags under OTHER_SWIFT_FLAGS, remembering to prepend “-D” to the setting. For example, to conditionally compile with a MYFLAG value:

    #if MYFLAG1
        // stuff 1
    #elseif MYFLAG2
        // stuff 2
    #else
        // stuff 3
    #endif
    

    The value to add to the setting -DMYFLAG

    Now we only need to pass the value MYFLAG to the new setting. Time to move all those conditional compilation values!

    Please refer to below link for more Swift Build Settings feature in Xcode 8: http://www.miqu.me/blog/2016/07/31/xcode-8-new-build-settings-and-analyzer-improvements/

提交回复
热议问题