1.處理NSLog事件(開發(fā)模式打印,發(fā)布模式不打印)
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:FILE] lastPathComponent] UTF8String], LINE, [[NSString stringWithFormat:FORMAT, ##VA_ARGS] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif
2.在OC文件導(dǎo)入某些頭文件
#ifdef OBJC
//導(dǎo)入頭文件
#endif
3.處理循環(huán)引用問題(處理當(dāng)前類對(duì)象)
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
4.獲取屏幕寬高
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
5.獲取系統(tǒng)版本
#define kVersion [[UIDevice currentDevice].systemVersion floatValue]
6.設(shè)置顏色RGB值+透明度
#define kRGBA(a,b,c,d) [UIColor colorWithRed:(a/255.0) green:(b/255.0) blue:(c/255.0) alpha:d]
7.設(shè)置隨機(jī)顏色
#define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
8.設(shè)置view的圓角邊框
#define kViewBorderRadius(View, Radius, Width, Color)
[View.layer setCornerRadius:(Radius)];
[View.layer setMasksToBounds:YES];
[View.layer setBorderWidth:(Width)];
[View.layer setBorderColor:[Color CGColor]];
9.獲取圖片資源
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]];
10.獲取當(dāng)前語言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
11.判斷當(dāng)前設(shè)備類型
#define kDeviceIsiPhone [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone
#define kDeviceIsiPad [UIDevice currentDevice].userInterfaceIdiom ==UIUserInterfaceIdiomPad
12.判斷是真機(jī)還是模擬器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
13.沙盒目錄文件
//獲取temp
#define kPathTemp NSTemporaryDirectory()
//獲取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//獲取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
14.宏與const 的使用
宏的用法:一般字符串抽成宏潮梯,代碼抽成宏使用宛逗。
const用法:一般常用的字符串定義成const(對(duì)于常量字符串蘋果推薦我們使用const)褐缠。
宏與const區(qū)別:
1.編譯時(shí)刻不同儡毕,宏屬于預(yù)編譯 ,const屬于編譯時(shí)刻
2.宏能定義代碼咙咽,const不能玛瘸,多個(gè)宏對(duì)于編譯會(huì)相對(duì)時(shí)間較長(zhǎng),影響開發(fā)效率翠桦,調(diào)試過慢横蜒,const只會(huì)編譯一次,縮短編譯時(shí)間销凑。
3.宏不會(huì)檢查錯(cuò)誤丛晌,const會(huì)檢查錯(cuò)誤
通過以上對(duì)比,我們以后在開發(fā)中如果定義一個(gè)常量字符串就用const斗幼,定義代碼就用宏澎蛛。
static NSString *const loginAccount = @"loginAccount";
static NSString *const loginPassword = @"loginPassword";
原文地址 上吊的豆腐簡(jiǎn)書