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

前端 未结 4 970
故里飘歌
故里飘歌 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 21:49

    Generally,

    static NSString * const
    

    is a better choice.

    Actually, static NSString const * is same to static NSString *, cause the string here is already immutable. If you analyse it deeper, you'll notice that const is absolutely nothing to do with it, NSString is Objective-C's class, it wraps the actual value in C.

    Note: NSString is a constant type itself (there's a NSMutableString exists). You only need to define a const pointer for it if u want it to be a constant.


    EDIT

    static NSString * const var;       // 1
    static NSString const * const var; // same to 1, first const is useless
    static const NSString * const var; // same to 1, first const is useless
    

    CANNOT do any modification.

    static NSString * var;              // 2
    static NSString const * var;        // same to 2, the const is useless
    static const NSString * var;        // same to 2, the const is useless
    

    CANNOT modify the value of var, but CAN modify the pointer.

    static NSMutableString * const var; // 3
    

    CAN modify the value of var, but CANNOT for the pointer.

    static NSMutableString * var; // 4
    

    CAN modify both the value & pointer.


    EDIT 2

    As @user3125367 mentioned,

    Immutability in Objective-C terms has nothing to do with constness in C.
    ...
    There are 3 orthogonal things: pointer constness, object field constness and object's high-level mutability.

    I agree with him about it. NSString has a higher level (it also inherited form NSObject), const on it should have no effect in fact (not the same meaning about the "no effect on immutable object"). But the complier might take care of it already.

    NOTE:

    var = @"a";  
    var = @"b";  
    

    the code snippet above means the pointer changed, not the value. There's no way to modify the value of NSString instance (for NSMutableString, you can use some methods like -appendString: to modify the value).

    If you use

    static NSString const * var;
    

    the final var will point to @"b". Instead, if you use

    static NSString * const var;
    

    compiler will throw an error, and it's what we want: making the var unchangeable.

    0 讨论(0)
  • 2021-02-09 21:58
    NSString const *str
    

    means str is immutable.if you change str value for example str=str2, there's error occurs.

    NSString *const str
    

    means what str point to is immutable.You can change str = str2;

    since NSString is immutable

    NSString *const str 
    

    is equal to

    NSString *str
    
    0 讨论(0)
  • 2021-02-09 22:07

    // Demo.h

    FOUNDATION_EXPORT NSString *const MyFirstConstant;
    FOUNDATION_EXPORT NSString *const MySecondConstant;
    

    // Demo.m

    NSString *const MyFirstConstant = @"FirstConstant";
    NSString *const MySecondConstant = @"SecondConstant";
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题