#ifdef replacement in the Swift language

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

提交回复
热议问题