[英]Conditional compile when running in Simulator as opposed to on a device
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
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
# ELSE
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
# END
EDIT 编辑
#if TARGET_IPHONE_SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#else
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#endif
Update: (Deprecated/Obsolete) This only worked for a few years, and does not work any more. 更新:(已弃用/已过时)这仅工作了几年,并且不再有效。 (10+ years later) (10多年后)
For the record, here's another method which Apple uses in some of their official Sample Code: 为了记录,这是Apple在其官方示例代码中使用的另一种方法:
#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
For those looking for a modern Swift solution, the (new) platform condition targetEnvironment
provides the definitive answer here. 对于那些寻找现代Swift解决方案的人来说,(新)平台条件targetEnvironment
在这里提供了明确的答案。 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 . 目标环境平台条件功能由SE-0190引入,自Swift 4.1起可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.