What is a better practice when defining a const NSString object in objective-c?

前端 未结 4 969
故里飘歌
故里飘歌 2021-02-09 21:21

When i read colleagues\' code in my team, i usually found there were two different types of definition for const NSString objects:

static NSString const *

4条回答
  •  孤独总比滥情好
    2021-02-09 22:12

    I prefer static const NSString *var; because staticand const are qualifiers for the NSString pointer. Only the pointer is const not the NSString.

    The NSString will be constant because it is not mutable but a const NSMutableString *s will not make s mutable String become constant and so not modifiable. The pointer to the mutable String s will be constant and the compiler won't allow you to modify it.

    Sorry for the inversion: Ken Thomases is right: int * const Var; means the pointer is constant and cannot be changed, but the data it points to can be changed. const int * Var means the data pointed to by Var cannot be changed.

提交回复
热议问题