在Objective-C中定义const NSString对象时有什么更好的做法? [英] What is a better practice when defining a const NSString object in objective-c?

查看:109
本文介绍了在Objective-C中定义const NSString对象时有什么更好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在团队中阅读同事的代码时,我通常发现const NSString对象有两种不同的定义类型:

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

据我所知,前者是指向const NSString对象的指针,而后者则定义了指向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.

我的问题是,在objective-c中定义const NSString对象时,哪一种是更好的编程实践和首选方式?

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

谢谢.

推荐答案

通常

static NSString * const

是更好的选择.

实际上,static NSString const *static NSString *相同,因为此处的字符串已经不可变. 如果您对其进行更深入的分析,您会发现const与它完全无关,NSString是Objective-C的类,它将实际值包装在C中.

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.

注意:NSString本身是常量类型(存在NSMutableString).如果您希望它为常量,则只需为其定义一个const指针.

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.

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的值,但是 CAN 修改指针.

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

static NSMutableString * const var; // 3

CAN 修改var的值,但不能修改指针的值.

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

static NSMutableString * var; // 4

CAN 修改值&指针.

CAN modify both the value & pointer.

如@ user3125367所述,

As @user3125367 mentioned,

Objective-C术语的不可变性与C的常数性无关.
...
正交性有3种:指针常量,对象字段常量和对象的高级可变性.

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应该不起作用(关于对不可变对象不起作用" 的含义不相同). 但是编译器可能已经解决了.

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.

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

上面的代码段表示指针已更改,而不是值.无法修改NSString实例的值(对于NSMutableString,可以使用诸如-appendString:的某些方法来修改值).

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).

如果您使用

static NSString const * var;

最后的var将指向@"b".相反,如果您使用

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

static NSString * const var;

编译器将抛出一个错误,这就是我们想要的:使var不可更改.

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

这篇关于在Objective-C中定义const NSString对象时有什么更好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆