static NSString usage vs. inline NSString constants

前端 未结 6 670
独厮守ぢ
独厮守ぢ 2021-01-30 09:03

In Objective-C, my understanding is that the directive @\"foo\" defines a constant NSString. If I use @\"foo\" in multiple places, the same immutable NSString object is referenc

6条回答
  •  野性不改
    2021-01-30 09:22

    It's good practice to turn literals into constants because:

    1. It helps avoid typos, like you said
    2. If you want to change the constant, you only have to change it in one place

    I prefer using static const NSString* static NSString* const because it's slightly safer than #define. I tend to avoid the preprocessor unless I really need it.

提交回复
热议问题