Ios 在模拟器中运行而不是在设备上运行时进行条件编译
当针对模拟器而不是我的设备时,是否有编译器指令可用于编译不同的代码行。比如:Ios 在模拟器中运行而不是在设备上运行时进行条件编译,ios,objective-c,iphone,ios-simulator,compiler-directives,Ios,Objective C,Iphone,Ios Simulator,Compiler Directives,当针对模拟器而不是我的设备时,是否有编译器指令可用于编译不同的代码行。比如: # IF SIMULATOR [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; # ELSE [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; # END 编辑 更新:(已弃用/过时)这只适用于几年,不再适用。(
# IF SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
# ELSE
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
# END
编辑
更新:(已弃用/过时)这只适用于几年,不再适用。(10多年后)
为了记录在案,苹果在一些官方示例代码中使用了另一种方法:
#if TARGET_CPU_ARM
// Only executes on an iPhone or iPod touch device
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#else
// Only executes on the Simulator
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#endif
对于那些寻求现代Swift解决方案的人来说,(新)平台条件
targetEnvironment
在此提供了明确的答案。例如:
#如果目标环境(模拟器)
self.imagePicker.sourceType=.photoLibrary
#否则
self.imagePicker.sourceType=.camera
#恩迪夫
target environment platform condition(目标环境平台条件)功能由Swift 4.1引入并自推出以来一直可用。在这种情况下,苹果的示例代码似乎是错误的,如果他们更改了设备(或Mac)的架构,该代码将被破坏。
#if TARGET_CPU_ARM
// Only executes on an iPhone or iPod touch device
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#else
// Only executes on the Simulator
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#endif