OC基本功(四)使用const修饰变量

用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;




  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值