zoukankan      html  css  js  c++  java
  • OC 之 const

    1. 修饰变量

      一般设置传参数的时候 若设置为const, 则在调用过程中不允许修改参数值;(readonly)

     1  // *前const: 不能通过指针, 改变p指向的值
     2     const int *p = &age;
     3     // 错误写法
     4     *p = num;
     5     
     6     
     7     
     8     // *后const: 指针不能指向其他变量
     9     int * const q = #
    10     // 错误写法
    11     q = &age;

    2. 用在iOS项目中, 修饰通知

    宏(#define)在编译过程中完成代码替换(项目中调用几次, 编译的时候宏定义就会出现几次);

    const(全局常量)在编译中调用的是同一个内存的同一个常量;

      [注意]: 在.pch文件中如果用const定义常量, 则会报链接错误(link command error), 因为编译时相当于, 每个调用了该变量的文件中都会编译一次, 所以报multi_define错误; 需要借助extern关键字来引用.

    static: (静态变量)存储在静态存储区, 只有当前类能够访问;

     1 .h文件 2 
     3 /**
     4  *  点击表情键盘中的表情按钮通知
     5  */
     6 extern NSString *const PPEmotionBtnDidSelectedNoticefication;
     7 extern NSString *const PPEmotionBtnDidSelectedKey;
     8 
     9 /**
    10  *  删除按钮通知
    11  */
    12 extern NSString *const PPEmotionCancelBtnDidSelectedNoticefication;
    13 extern NSString *const PPEmotionCancelBtnDidSelectedKey;
    14 
    15 
    16 
    17 
    18 
    19 .m文件20 #import <Foundation/Foundation.h>
    21 NSString *const PPEmotionBtnDidSelectedNoticefication = @"PPEmotionBtnDidSelectedNoticefication";
    22 NSString *const PPEmotionBtnDidSelectedKey = @"PPEmotionBtnDidSelectedKey";
    23 
    24 NSString *const PPEmotionCancelBtnDidSelectedNoticefication = @"PPEmotionCancelBtnDidSelectedNoticefication";
    25 NSString *const PPEmotionCancelBtnDidSelectedKey = @"PPEmotionCancelBtnDidSelectedKey";
    26 
    
  • 相关阅读:
    c++ 图解快速排序算法
    Shell脚本检测文件夹是否已被挂载的方法
    Linux使用mount挂载samba共享
    PHP使用字符串名称调用类的方法
    命令行查看端口号被进程占用
    Golang Clearing slice
    送给自己的程序员箴言
    Entity Framework6 with Visual Studio 2013 update3 for Oracle 11g
    深入浅出ASP.NET MVC5系列之一
    年终福利:调试.NET Framework源代码
  • 原文地址:https://www.cnblogs.com/guangleijia/p/5167265.html
Copyright © 2011-2022 走看看