const vs static NSStrings in Objective-C

后端 未结 3 1728
情歌与酒
情歌与酒 2020-12-07 16:41

These lines are both in the implementation file above the @implementation declaration.

NSString * const aVar = @\"aVarStringValue\";

static NSS         


        
3条回答
  •  自闭症患者
    2020-12-07 17:21

    The static only changes the scope of the variable, not how it is declared or stored.

    In both cases, the compiler will create a constant version of the NSString instance that is stored in the mach-o file. Thus, there is only ever one instance of either (note that you can change the behavior to cause the string to be dynamically created on load of the mach-o, but there is still just one instance).

    The static just marks the aVar variable as being visible within the scope of the compilation unit -- the file -- only. Without the static, you could redeclare the string as extern NSString *aVar; in a header somewhere and have access to it from anywhere.

    The const is orthogonal and, in the case of of NSString reference is pretty much entirely irrelevant.

提交回复
热议问题