多用类型常量,少用#define 预处理指令

2016-04-14  本文已影响19人  Mr_Kong

简介


在 Objective - C 中我们经常会用到#define来进行宏定义,但是殊不知宏定义也会给我们带来一系列的影响。

异同


例:
 #define ANIMATION_DURATION 0.1
 static const NSTimeInterval kAnimationDuration = 0.1;

用法


1.0 定义
.m文件中
#imprt "EOCAnimationView.h"
static const NSTimeInterval kAnimationDuration = 0.1;
@implementation EOCAnimationView
@end
2.0 定义(也可以这样定义)
.h
extern const NSTimeInterval EOCAnimationDuration
.m
const NSTimeInterval EOCAnimationDuration = 0.1;
3.0 通知(这样写的好处是外界可以调通知但是内部又做了保护)
.h
extern NSString *const EOCNotification
.m
NSString *const EOCNotification = "Notification"

上一篇 下一篇

猜你喜欢

热点阅读