What is the best practice to write the debugging and production code at the same time?

I develop iOS apps and use swift and Xcode now. I often write like this when I use a test code for debug.

//test
label.backgroundColor = UIColor.red
//set bicolor
label.backgroundColor = UIColor.clear

If it's test mode (before release app), I want to set backgroundColor in red. When I submit the app to store, the test code that set backgroundColor to red will be comment out and The backgroundColor will be set clear.

What is the best practice to write the debugging and production code at the same time?. Is the abouve is good practice or not?.

4 answers

  • answered 2018-04-17 05:25 Ankit Jayaswal

    There are 3 #pragma_mark available in swift same as Objective-C:

    1. // MARK: - write your text here

    2. // TODO: - write your text here

    3. // FIXME: - write your text here

    You can use - to add separator line.

  • answered 2018-04-17 05:25 Subin Babu

    In Swift you can use the following code:

    #if RELEASE
        // release only code
    #else
        // debug only code
    #endif
    

    I normally write following method in Android

    If you want some line of code for debugging and production, you can do the following,

    Create a global boolean value like below in GlobalVariableClass,

    public boolean debug=true

    Then you can write code like,

    if(GlobalVariableClass.debug){
        //debug code
    }else{
       //Production code
    }
    

    It also helpfull for log data as ,

    If(GlobalVariableClass.debug){
       Log.e("TAG","log message");
    }
    

    So for the production time, you need to change the debug in GlobalVariableClass to false

  • answered 2018-04-17 05:25 Prateek kumar

    Using Swift flags DEBUG and RELEASE, you can separate the code for DEBUG and RELEASE

    https://kitefaster.com/2016/01/23/how-to-specify-debug-and-release-flags-in-xcode-with-swift/

  • answered 2018-04-17 05:25 Vollan

    What you can do is use if development

    label.backgroundColor = UIColor.clear
    #if DEVELOPMENT
    label.backgroundColor = UIColor.red
    #endif
    

    or

    label.backgroundColor = UIColor.red
    #if RELEASE
    label.background = UIColor.clear
    #endif