const vs static NSStrings in Objective-C

后端 未结 3 1729
情歌与酒
情歌与酒 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:23

    static keyword in Objective-C (and C/C++) indicates the visibility of the variable. A static variable (not in a method) may only be accessed within that particular .m file. A static local variable on the other hand, gets allocated only once.

    const on the other hand, indicates that the reference may not be modified and/or reassigned; and is orthogonal on how it can be created (compilers may optimize consts though).

    It's worth mentioning that NSString literals get initialized and never get destroyed in the life of application. They are allocated in a read-only part of the memory.

提交回复
热议问题