在模拟器中运行时而不是在设备上运行时进行条件编译

发布于 2024-07-19 04:47:01 字数 522 浏览 10 评论 0原文

当针对模拟器而不是我的设备时,是否有一个编译器指令可以用来编译不同的代码行。 类似于:

# IF SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
# ELSE
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
# END

编辑

直接链接到文档。

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

Direct link to docs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吝吻 2024-07-26 04:47:01
#if TARGET_IPHONE_SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#else
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#endif
#if TARGET_IPHONE_SIMULATOR
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
#else
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
#endif
油饼 2024-07-26 04:47:01

更新:(已弃用/已过时)这只有效了几年,并且不再有效。 (10 多年后)

根据记录,这是 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

Update: (Deprecated/Obsolete) This only worked for a few years, and does not work any more. (10+ years later)

For the record, here's another method which Apple uses in some of their official Sample Code:

#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
沉溺在你眼里的海 2024-07-26 04:47:01

对于那些寻求现代 Swift 解决方案的人来说,(新)平台条件 targetEnvironment 在这里提供了明确的答案。 例如:

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

目标环境平台条件功能是由SE-0190,自 Swift 4.1 起可用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文