#ifdef replacement in the Swift language

前端 未结 17 1719
名媛妹妹
名媛妹妹 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:27

    In Swift projects created with Xcode Version 9.4.1, Swift 4.1

    #if DEBUG
    #endif
    

    works by default because in the Preprocessor Macros DEBUG=1 has already been set by Xcode.

    So you can use #if DEBUG "out of box".

    By the way, how to use the condition compilation blocks in general is written in Apple's book The Swift Programming Language 4.1 (the section Compiler Control Statements) and how to write the compile flags and what is counterpart of the C macros in Swift is written in another Apple's book Using Swift with Cocoa and Objective C (in the section Preprocessor Directives)

    Hope in future Apple will write the more detailed contents and the indexes for their books.

    0 讨论(0)
  • 2020-11-22 11:28

    Moignans answer here works fine. Here is another peace of info in case it helps,

    #if DEBUG
        let a = 2
    #else
        let a = 3
    #endif
    

    You can negate the macros like below,

    #if !RELEASE
        let a = 2
    #else
        let a = 3
    #endif
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-22 11:30

    ![In Xcode 8 & above go to build setting -> search for custom flags ]1

    In code

     #if Live
        print("Live")
        #else
        print("debug")
        #endif
    
    0 讨论(0)
  • 2020-11-22 11:34
    func inDebugBuilds(_ code: () -> Void) {
        assert({ code(); return true }())
    }
    

    Source

    0 讨论(0)
  • 2020-11-22 11:37

    After setting DEBUG=1 in your GCC_PREPROCESSOR_DEFINITIONS Build Settings I prefer using a function to make this calls:

    func executeInProduction(_ block: () -> Void)
    {
        #if !DEBUG
            block()
        #endif
    }
    

    And then just enclose in this function any block that I want omitted in Debug builds:

    executeInProduction {
        Fabric.with([Crashlytics.self]) // Compiler checks this line even in Debug
    }
    

    The advantage when compared to:

    #if !DEBUG
        Fabric.with([Crashlytics.self]) // This is not checked, may not compile in non-Debug builds
    #endif
    

    Is that the compiler checks the syntax of my code, so I am sure that its syntax is correct and builds.

    0 讨论(0)
提交回复
热议问题