Objective-C : #define vs extern const

后端 未结 4 918
时光说笑
时光说笑 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

    A #define defines a macro which is replaced before compilation starts where as extern *** *const merely modifies a variable so that the compiler will flag an error if you try to change it. There are some cases in that you would use a #define because you can't use a extern *** *const. In theory a extern *** *const will take up memory and requires a reference to memory but this is insignificant as it maybe optimized away from the compiler.

    extern *** *consts are a lot more compiler and debug friendlier then #defines this can be the deciding point when you decide which one to use.

    Some see that pre-processor directives like #define are frowned upon which would suggest you should be using extern *** *const over #define

    But whilst the pre-processor is frowned open some say it is more secure then a variable as it can't be changed at runtime whereas a variable can.

    Both have there advantages and disadvantages and I don't think (I can't find anything myself) that Apple recommends one over the other. My personal opinion is to use a mix of them both using a pre-processor directive #define over a extern *** *const where it would seem more beneficial, this is what I do.

提交回复
热议问题