用const修饰的变量表示变量值只读。
static有两个作用:指定变量的作用域和存储的方式。例如在一个文件首部声明static变量,那么这个变量只能在该文件中使用。
两者结合可以设置作用域确定并且只读的变量,例如文件中的全局常量。
1.用const修饰指针变量
(1)const 类型 * 变量名:可以改变指针的指向,不能改变指针指向的内容。
int x = 1;
int y = 2;
const int *px = &x; // 让指针px指向变量x
px = &y; // 改变指针px的指向,使其指向变量y
*px = 3; // 改变px指向的变量x的值,出错:Read-only variable is not assignable
(2)类型 * const 变量名:可以改变指针指向的内容,不能改变指针的指向。
int x = 1;
int y = 2;
int * const px = &x; // 让指针px指向变量x
px = &y; // 改变px的指向,出错:Read-only variable is not assignable
(*px) += 2; // 改变px指向的变量x的值
(3)const 类型 * const 变量名:指针的指向、指针指向的内容都不可以改变。
int x = 1;
int y = 2;
const int * const px = &x; // 让指针px指向变量x
px = &y; // 改变px的指向,出错:Read-only variable is not assignable
(*px) += 2; // 改变px指向的变量x的值,出错:Read-only variable is not assignable
2.创建文件域的字符串常量
如果指针指向的是可变的字符串对象,即使加const修饰该对象,该对象也是可变的,如:
// 定义指针str,指向只读对象
const NSMutableString *str = [NSMutableString stringWithString:@"Hello"];
NSLog(@"%@", str);
NSLog(@"%p", str);
NSLog(@"%p", &str);
NSLog(@"---------------");
// 改变str指向的对象的值
[str appendString:@" World"];
NSLog(@"%@", str);
NSLog(@"%p", str);
NSLog(@"%p", &str);
NSLog(@"---------------");
// 改变str的指向
str = [NSMutableString stringWithString:@"A new string"];
NSLog(@"%@", str);
NSLog(@"%p", str);
NSLog(@"%p", &str);
NSLog(@"---------------");
输出结果为:
2014-02-02 20:48:54.987 ConstDemo[3512:303] Hello
2014-02-02 20:48:54.989 ConstDemo[3512:303] 0x1002041a0
2014-02-02 20:48:54.989 ConstDemo[3512:303] 0x7fff5fbff888
2014-02-02 20:48:54.989 ConstDemo[3512:303] ---------------
2014-02-02 20:48:54.990 ConstDemo[3512:303] Hello World
2014-02-02 20:48:54.990 ConstDemo[3512:303] 0x1002041a0
2014-02-02 20:48:54.990 ConstDemo[3512:303] 0x7fff5fbff888
2014-02-02 20:48:54.990 ConstDemo[3512:303] ---------------
2014-02-02 20:48:54.991 ConstDemo[3512:303] A new string
2014-02-02 20:48:54.991 ConstDemo[3512:303] 0x100103580
2014-02-02 20:48:54.991 ConstDemo[3512:303] 0x7fff5fbff888
2014-02-02 20:48:54.992 ConstDemo[3512:303] ---------------
如果指向的是不可变对象如NSString,就不用担心指向的对象的值被改变了,但是指针的指向也可以被改变:
// 定义指针str,指向只读对象
const NSString *str = @"Hello";
NSLog(@"%@", str);
NSLog(@"%p", str);
NSLog(@"%p", &str);
NSLog(@"---------------");
// 改变str的指向
str = @"A new string";
NSLog(@"%@", str);
NSLog(@"%p", str);
NSLog(@"%p", &str);
NSLog(@"---------------");
2014-02-02 20:52:45.262 ConstDemo[3532:303] Hello
2014-02-02 20:52:45.263 ConstDemo[3532:303] 0x100001040
2014-02-02 20:52:45.264 ConstDemo[3532:303] 0x7fff5fbff888
2014-02-02 20:52:45.264 ConstDemo[3532:303] ---------------
2014-02-02 20:52:45.264 ConstDemo[3532:303] A new string
2014-02-02 20:52:45.264 ConstDemo[3532:303] 0x1000010c0
2014-02-02 20:52:45.265 ConstDemo[3532:303] 0x7fff5fbff888
2014-02-02 20:52:45.265 ConstDemo[3532:303] ---------------
如果想创建一个只读的字符串常量,应该设定指针只读,并指向不可变对象:
NSString * const str = @"Hello";
// NSString对象本身是不可变的
// 改变str的指向,错误:Read-only variable is not assignable
str = @"A new string";
那么声明文件域的字符串常量的做法是:在文件首部作以下声明
static NSString * const gString = @"Global";
必须在声明时就对常量赋值。否则在使用时不能再对其赋值,从而使用的是指向nil的指针。
如果要声明文件域的是基本数据类型常量,在文件首部声明变量时用static const修饰,并赋初值。例如:
static const NSUInteger gInteger = 100;