#ifdef replacement in the Swift language

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

    There are some processors that take an argument and I listed them below. you can change the argument as you like:

    #if os(macOS) /* Checks the target operating system */
    
    #if canImport(UIKit) /* Check if a module presents */
    
    #if swift(<5) /* Check the Swift version */
    
    #if targetEnvironment(simulator) /* Check envrionments like Simulator or Catalyst */
    
    #if compiler(<7) /* Check compiler version */
    

    Also, You can use any custom flags like DEBUG or any other flags you defined

    #if DEBUG
    print("Debug mode")
    #endif
    

提交回复
热议问题