OC、Swift设置编译条件

在 OC 中我们设置编译条件,一般通过修改

image

Preprocessor Macros 中对应的宏定义。但是在Swift中,预处理的宏定义已经无效了,主要是 Swift 不在包含预处理的过程

The Swift compiler does not include a preprocessor. Instead, it takes advantage of compile-time attributes, build configurations, and language features to accomplish the same functionality. For this reason, preprocessor directives are not imported in Swift.

那么我们该如何在编译时做条件选择呢?

Swift中设置编译条件

在 Xcode 8 之前我们可以通过设置

image

Swift Compiler - Custom Flags 来设置我们需要用到的条件,在 Other Swift Flags 中设置 -D ADHOC

  1. Go to your project / select your target / Build Settings / search for Custom Flags
  2. For your chosen target set your custom flag using -D prefix (without white spaces), for both Debug and Release
  3. Do above steps for every target you have

在 Xcode 8 之后我们可以直接设置

image

Stackoverdflow 上边的回答

image
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,037评论 3 119
  • 在临睡醒前做了一个梦,梦见我和一队人在玩踢纸片的游戏,那些人是我熟悉的队友,我们所有人要将有点硬度的纸片踢进地面上...
    花花公主阅读 1,618评论 0 1
  • 图片发自简书App 天空爱大地,日日夜夜不分离 呼出光的激情,唤醒沉睡的沙漠腹地 呼出云雨雪花,投入江河湖海的怀里...
    王巧霞阅读 1,579评论 0 0
  • 海峰阅读阅读 1,859评论 0 0
  • 飞鸟藏树林, 行者闻其鸣。 芳草享树荫, 小花丛中隐。 …… 后记:却道天暖好个秋!!
    大狗少一阅读 871评论 2 0