Conditional compile when running in Simulator as opposed to on a device

后端 未结 3 515
[愿得一人]
[愿得一人] 2021-02-01 03:06

Is there a compiler directive I can use to compile a different line of code when targetting the simulator as opposed to my device. Something like:

# IF SIMULATOR         


        
3条回答
  •  故里飘歌
    2021-02-01 03:57

    For those looking for a modern Swift solution, the (new) platform condition targetEnvironment provides the definitive answer here. For instance:

    #if targetEnvironment(simulator)
    self.imagePicker.sourceType = .photoLibrary
    #else
    self.imagePicker.sourceType = .camera
    #endif 
    

    The target environment platform condition feature was introduced by SE-0190 and is available since Swift 4.1.

提交回复
热议问题