Objective-C : #define vs extern const

后端 未结 4 858
时光说笑
时光说笑 2021-02-19 22:37

I know this question has been asked before, but I can\'t seem to find information about it in Apple\'s documentation; maybe some of you guys did.

A lot of Objective-C co

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 23:20

    If you have some global constants, for example in a Constants.h which is imported in your prefix header and you're using a #define macro for these constants it's going to rebuild your whole project if you make any changes to these constants. In that case it is better to split your constants and use extern for strings, integers and everything else that you can use extern for.

    For example if you have extern NSString *const kServerURL; and you change your server address it's not going to rebuild your whole project but if you use define there, it's going to rebuild it. So the only purpose at least for me is for optimising the compile time.

提交回复
热议问题