本文中并未包含所有的宏定義,只是部分我在開發過程中使用到的宏定義,在此跟大家分享一下
#define APPNAME? [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
#define APPVERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
#define APPBUILDING? [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
#define APPICON @"app icon的名稱"
#define APPSLOGAN @"app的口號"
#define NORMALIMAGE @"默認圖片名稱"
#define NORMALHEADIMAGE @"默認頭像名稱"
#define USERID [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"]//取用戶ID
#define TOKEN [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]//取token
//當前城市
#define LOCALCITY [[NSUserDefaults standardUserDefaults] objectForKey:@"localCity"]
//系統版本
#define? DEVICEVERSION [[UIDevice currentDevice].systemVersion floatValue]
//手機型號
#define PHONEMODEL [[UIDevice currentDevice] model]
//自定義顏色宏
#define ColorRGB(x, y, z, alp) [UIColor colorWithRed:(x)/255.0green:(y)/255.0blue:(z)/255.0alpha:(alp)]
#define? color255255255 [UIColor colorWithRed:(255)/255.0green:(255)/255.0blue:(255)/255.0alpha:(1)]
#define appBackgroundColor [UIColor colorWithRed:(247)/255.0green:(247)/255.0blue:(247)/255.0alpha:(1)]
//notice:APP中常用的背景色,字體顏色和視圖顏色最好用宏定義,方便后期整體風格變更時快速修改
//屏幕寬度
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height//屏幕高度
//狀態欄高度
#define statusHeight [[UIApplication sharedApplication] statusBarFrame].size.height
//屏幕寬高比
#define Width1 [UIScreen mainScreen].bounds.size.width/375
#define Height1 ([UIScreen mainScreen].bounds.size.height-statusHeight)/(667-20)
//弱引用/強引用
#define LRWeakSelf(type)? __weak typeof(type) weak##type = type;
#define LRStrongSelf(type)? __strong typeof(type) type = weak##type;
//設置 view 圓角和邊框
#define CornerViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
//本地存儲
//saveObject
#define SAVE_OBJECT(object,key)\
\
[[NSUserDefaults standardUserDefaults] setObject:(object) forKey:(key)];\
[[NSUserDefaults standardUserDefaults] synchronize];\
//取object
#define OBJECT_FOR_KEY(key)\
\
[[NSUserDefaults standardUserDefaults] objectForKey:(key)];
//saveInteger
#define SAVE_INTEGER(integer,key)\
\
[[NSUserDefaults standardUserDefaults] setInteger:(integer) forKey:(key)];\
[[NSUserDefaults standardUserDefaults] synchronize];\
//取integer
#define INTEGER_FOR_KEY(key)\
\
[[NSUserDefaults standardUserDefaults] integerForKey:(key)];
//saveBool
#define SAVE_BOOL(bool,key)\
\
[[NSUserDefaults standardUserDefaults] setBool:(bool) forKey:(key)];\
[[NSUserDefaults standardUserDefaults] synchronize];\
//取bool值
#define BOOL_FOR_KEY(key)\
\
[[NSUserDefaults standardUserDefaults] boolForKey:(key)];
//saveFloat
#define SAVE_FLOAT(float,key)\
\
[[NSUserDefaults standardUserDefaults] setFloat:(float) forKey:(key)];\
[[NSUserDefaults standardUserDefaults] synchronize];\
//取float
#define FLOAT_FOR_KEY(key)\
\
[[NSUserDefaults standardUserDefaults] floatForKey:(key)];
//Alert
#define ALERT(title,msg,cancelText) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:cancelText otherButtonTitles:nil] show]
//獲取window
?#define LXWINDOW? [[UIApplication sharedApplication].delegate window]
//debug模式下打印日志
//#ifdef DEBUG
//#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
//#else
//#define DLog(...)
//#endif
#ifdef DEBUG
#define DEBUGDate @" 5-9 "http://debug時顯示打包的日期
#define DLog( s, ... ) printf("class: <%p %s:(%d) > method: %s \n%s\n", self, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(s), ##__VA_ARGS__] UTF8String] );
#else
#define DEBUGDate @""
#define DLog( s, ... )
#endif
// 防止按鈕多次調用,給一個等待的時間seconds
#define lxPreventRepeatClickTime(_seconds_) \
static BOOL shouldPrevent; \
if (shouldPrevent) return; \
shouldPrevent = YES; \
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((_seconds_) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ \
shouldPrevent = NO; \
}); \
其他的比如網絡請求,常用控件可以專門二次封裝一下,像UITableview,UICollectionView這些必須要實現datasource和delegate的控件也應該封裝起來,工程前期多寫一些基類(如controller,view,model)和工具類,后期開發速度會事半功倍