码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发总结(A0)- #define 与 const

时间:2015-06-30 21:48:45      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

什么时候用#define ,什么时候用const ?

effective objective c item4 中这样说:

Prefer Typed Constants to Preprocessor #define

1. 避免 #define. 因为无类型信息,且可能会redefined

2. 在m文件中定义为 static const(实际上这种处理和#define一样)

3. 对于global constants,应在头文件中申明,在相关的m文件中定义,前缀为响应的类名称

如在.h 中,

extern const NSTimerInterval ClassAnimationDuration;

在.m中,

const NSTimerInterval ?ClassAnimationDuration=0.3;

(正如uiapplication 中各种notificaiton一样)

?

=====================

注意const 的位置,修饰const之前的关键字,如

static NSString * const CONSTANT; 是指 const pointer to an NSString

=====================

iOS开发总结(A0)- #define 与 const

标签:

原文地址:http://www.cnblogs.com/beddup/p/4611522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!