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

什么时候用 #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一样)

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

按 apple 官方 api 的风格来做,一般都是ok 的

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