When to use static string vs. #define

前端 未结 6 1809
-上瘾入骨i
-上瘾入骨i 2021-01-30 10:49

I am a little confused as to when it\'s best to use:

static NSString *AppQuitGracefullyKey = @\"AppQuitGracefully\";

instead of



        
6条回答
  •  一向
    一向 (楼主)
    2021-01-30 11:01

    If you use a static, the compiler will embed exactly one copy of the string in your binary and just pass pointers to that string around, resulting in more compact binaries. If you use a #define, there will be a separate copy of the string stored in the source on each use. Constant string coalescing will handle many of the dups but you're making the linker work harder for no reason.

提交回复
热议问题