-Preprocessor Macros : 预处理宏命令

应用场景

-今天有人问到我一个问题,怎么根据是真机还是模拟器来指定宏的不同定义.比如,debug模式下用测试的服务器网址,而release模式下,则使用线上正式服务器.

实例

1.定义一个宏

#if PROD == 1
// 正式服务器地址
const NSString * const SERVER_URL = @"http://www.abc.com/";
#else
// 测试服务器地址
const NSString * const SERVER_URL = @"http://beta.testAbc.com/";
#endif

2.进入'Build Settings' -> 搜索'Preprocessor Macros'

-然后如图所示进行设置
Preprocessor Macros

3.随后,可以进行测试

-可以在debug模式下,进行测试
-打印结果为

http://beta.testAbc.com/

-在Release模式下测试, (Edit Scheme -> Run -> Info -> Build Configuration ,把它改成 Release)
-随后,运行,打印结果为

http://www.abc.com/

相关文章:

  • 2022-12-23
  • 2022-03-03
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2021-10-13
  • 2021-09-15
猜你喜欢
  • 2021-12-26
  • 2021-11-13
  • 2021-09-29
  • 2021-08-25
  • 2022-12-23
  • 2021-07-23
  • 2022-02-08
相关资源
相似解决方案