找到你要的答案

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

Q:什么是一个更好的做法定义const NSString对象在Objective-C的时候? 注:

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 * and static NSString * const

As far as i know, the former is a pointer point to a const NSString object, and the latter defines a const pointer to a NSString object.

My question is which is a better programming practice and a preferred way when defining a const NSString object in objective-c?

Thx.

当我看到同事的代码在我的团队中,我经常发现有两种不同类型定义为const NSString对象:

static NSString const * and static NSString * const

据我所知,前者是一个指针指向const的NSString对象,后者定义const指针指向一个NSString对象。

我的问题是,这是一个更好的编程实践和首选的方式定义const NSString对象在Objective-C的时候?

谢谢.

answer1: 回答1:

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.

一般,

static NSString * const

是更好的选择。

实际上,静态NSString const *是静态的NSString *一样,因为这里已经是不可变的字符串。如果你分析它更深,你会发现,const是绝对与它无关,NSString是Objective-C的类,它把实际价值的C.

注:NSString是恒定的类型本身(有一个NSMutableString存在)。你只需要定义一个const指针如果你希望它是一个常数。


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

不能做任何修改。

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

不能修改var的值,但可以修改指针。

static NSMutableString * const var; // 3

可以修改var的值,但不能为指针。

static NSMutableString * var; // 4

可以修改值和指针。


EDIT 2

“user3125367提到,

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.

我同意他的意见。NSString具有较高的水平(它也继承了NSObject),const它事实上应该没有影响(不是同一意义的“对不可变对象的“无效果)。但是,编译器会照顾它已经。

注:

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

上面的代码段的指针改变,没有价值。有没有办法修改的NSString实例的值(NSMutableString,你可以用一些方法如appendstring:修改值)。

如果你使用

static NSString const * var;

最后的var将指向“b”。相反,如果你使用

static NSString * const var;

编译器将抛出一个错误,这就是我们想要的:使var不变。

answer2: 回答2:
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
NSString const *str

意味着你改变immutable.if STR是例如STR = str2 STR值有错误发生。

NSString *const str

是什么意思STR位点是不变的。你可以改变str = str2;

由于NSString是不变的

NSString *const str 

等于

NSString *str
answer3: 回答3:

// Demo.h

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

// Demo.m

NSString *const MyFirstConstant = @"FirstConstant";
NSString *const MySecondConstant = @"SecondConstant";

/演示程序

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

/示范性

NSString *const MyFirstConstant = @"FirstConstant";
NSString *const MySecondConstant = @"SecondConstant";
answer4: 回答4:

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.

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

NSString将常因为不可变但const NSMutableString是不会让可变的字符串成为常数,所以不可修改的。对可变的字符串的指针是恒定的,编译器不允许你修改它。

对不起,倒置:Ken Thomases是对的:int * const变量;意味着指针是恒定的,无法改变的,但它所指向的数据是可以改变的。const int * Var所指向的数据变量不能被改变。

ios  objective-c