When to use static string vs. #define

前端 未结 6 1810
-上瘾入骨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:22

    See "static const" vs "#define" vs "enum". The main advantage of static is type safety.

    Other than that, the #define approach introduces a flexibility of inline string concatenation which cannot be done with static variables, e.g.

    #define ROOT_PATH @"/System/Library/Frameworks"
    [[NSBundle bundleWithPath:ROOT_PATH@"/UIKit.framework"] load];
    

    but this is probably not a good style :).

提交回复
热议问题