首页 > 移动平台 > 详细

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

时间:2015-06-30 21:47:50      阅读:180      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!