Static constant Vs defines directive

I want to define a constant in my .m file. Here I see two options:

static NSString *const kMyLabel = @"myLabel"; #define kMyLabel @"myLabel" 

Which one is better? Are there any problems using static here?

+4
source share
2 answers

The only difference is that you can take the address of a variable, while you cannot take the address of a constant string expression (which is what the macro comes down to). I prefer to avoid #define whenever possible, so I would go with the first one, but this is just a matter of style.

+3
source

For the most part, this does not matter in terms of use. But there is a slight advantage to using the static method in that each use of the kMyLabel constant in the code will be a pointer to the same object (in most cases), while the #define method will create copies of the string. I believe that in later compilers it is reasonably reasonable to defines as a single object, but to be safe, I would simply use the static method.

+1
source

Source: https://habr.com/ru/post/1336854/


All Articles