美文网首页
const与宏的区别

const与宏的区别

作者: 仰天风吹雪 | 来源:发表于2017-03-21 08:46 被阅读14次
  1. 宏常见用法:
  • 常见的字符串写成宏
  • 常用的代码写成宏
  1. const(常量)
  • 当有字符串常量的时候, 苹果推荐我们使用const
  • 被const修饰的变量是只读的
  • const书写规范: 一定要放在变量的左边
     用const修饰基本变量
int const a = 10;
const int a = 10;
这两种写法没有区别, 表示a是只读变量

区别:

  1. 编译时刻: 宏, 预编译; const, 编译时刻
  2. 编辑检查: 宏, 不做检查, 只是替换, const, 会编译检查, 会报编译错误
  3. 宏的好处: 可以定义代码, const不能
    4, 宏的坏处: 使用大量宏, 容易造成编译时间过长, 因此常用的字符串通常使用const修饰
NSString *const TGSAccoutKey = @"count";
#define TGSAccountKey @"count";

    [TGSUserDefaults setObject:@"123" forKey:TGSAccoutKey];




相关文章

网友评论

      本文标题:const与宏的区别

      本文链接:https://www.haomeiwen.com/subject/frefnttx.html