如何在Xcode Build Setting中使用Compiler Flags

  • 重要提醒: 最好在一个Demo项目里进行这些演练的操作,因为一旦设置这些东西,project.pbxproj配置文件就会更改,而且没发Command+Z回退

我们可以通过设置Compiler Flags来定义宏,然后我们就可以在代码中使用这些宏,来进行条件编译的操作

  • 有三种方式设置:
OTHER_CFLAGS (Other C Flags)
// 在Target>Build Setting>Custom Compiler Flags>Other C Flags

GCC_PREPROCESSOR_DEFINITIONS (Preprocessor Macros)
// 在Target>Build Setting> Preprocessing > Preprocessor Macros

INFOPLIST_PREPROCESSOR_DEFINITIONS (Info.plist Preprocessor Definitions)
// 在Target>Build Setting> Packaging > Info.plist Preprocessor Definitions
  • 注意:在other c flags 设置 需要使用-D的格式
-D[你要设置的宏名称] 
// 没有中括号
  • Preprocessor Macros 设置定义宏需要转义,例如
// 定义替换为NSString类型
MY_MACRO=@\"Hello!!\"
// 这样定义替换后的类型是:NSNumber
MY_NUMBER=@"123"

你可能感兴趣的:(如何在Xcode Build Setting中使用Compiler Flags)