Xcode: TEST vs DEBUG preprocessor macros

后端 未结 11 919
余生分开走
余生分开走 2020-11-28 06:24

When creating a new project with unit tests, Xcode sets the build configuration to Debug for the Test scheme (same for the Run scheme).

Should I differentiate betwee

11条回答
  •  佛祖请我去吃肉
    2020-11-28 07:01

    If you create a Test build configuration and then set the "Other Swift Flags" property of your Target to "-DTEST" it will define a TEST macro that will work in your swift code. Make sure you set it in the build settings of your App target so that you can use it in your App's Swift code.

    Then with this set, you can test your code like so:

    func testMacro() {
       #if !TEST 
           // skipping over this block of code for unit tests
       #endif
    }
    

提交回复
热议问题