1.處理NSLog事件(開發者模式打印,發布者模式不打印)
#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語言的情況下導入某些頭文件
#ifdef __OBJC__ //導入頭文件 #endif
3.處理循環引用問題(處理當前類對象)
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
4.獲取屏幕寬高
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width #define ScreenHeight [[UIScreen mainScreen] bounds].size.heigh
5.判斷iOS8或更高系統版本(謹慎使用,floatValue是不靠譜的,具體原因請看:http://www.jianshu.com/p/528897755dc8)
#define IOS8UP ([[UIDevice currentDevice].systemVersion floatValue] >= 8)
6.設置顏色RGB值
#define RGB(a,b,c) [UIColor colorWithRed:(a/255.0) green:(b/255.0) blue:(c/255.0) alpha:1.0]
7.設置顏色RGB值+透明度
#define RGBA(a,b,c,d) [UIColor colorWithRed:(a/255.0) green:(b/255.0) blue:(c/255.0) alpha:d]
8.支持橫屏
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 當前Xcode支持iOS8及以上 #define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width) #define SCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height) #define SCREEN_SIZE ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale):[UIScreen mainScreen].bounds.size) #else #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height #define SCREEN_SIZE [UIScreen mainScreen].bounds.size #endif
9.設置隨機顏色
#define LRRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
10.設置view的圓角邊框
#define LRViewBorderRadius(View, Radius, Width, Color)\\ [View.layer setCornerRadius:(Radius)];\ [View.layer setMasksToBounds:YES];\ [View.layer setBorderWidth:(Width)];\ [View.layer setBorderColor:[Color CGColor]]
11.獲取圖片資源
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
12.獲取當前語言
#define LRCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
13.判斷當前的iPhone設備/系統版本
//判斷是否為iPhone #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) //判斷是否為iPad #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //判斷是否為ipod #define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"]) // 判斷是否為 iPhone 5SE #define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f // 判斷是否為iPhone 6/6s #define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f // 判斷是否為iPhone 6Plus/6sPlus #define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f //獲取系統版本 #define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
14.判斷是真機還是模擬器
#if TARGET_OS_IPHONE//iPhone Device#endif#if TARGET_IPHONE_SIMULATOR//iPhone Simulator#endif
15.沙盒目錄文件
//獲取temp #define kPathTemp NSTemporaryDirectory() //獲取沙盒 Document #define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] //獲取沙盒 Cache #define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
16.宏與const 的使用
很多小伙伴在定義一個常量字符串,都會定義成一個宏,最典型的例子就是服務器的地址。在此所有用宏定義常量字符的小伙伴以後就用const來定義吧!為什麼呢 ?我們看看:
宏的用法:一般字符串抽成宏,代碼抽成宏使用。
const用法:一般常用的字符串定義成const(對於常量字符串蘋果推薦我們使用const)。
宏與const區別:
1.編譯時刻不同,宏屬於預編譯 ,const屬於編譯時刻
2.宏能定義代碼,const不能,多個宏對於編譯會相對時間較長,影響開發效率,調試過慢,const只會編譯一次,縮短編譯時間。
3.宏不會檢查錯誤,const會檢查錯誤
通過以上對比,我們以後在開發中如果定義一個常量字符串就用const,定義代碼就用宏。
static NSString * const loginAccount = @"loginAccount";static NSString * const loginPassword = @"loginPassword";
17.單例化一個類
// // SynthesizeSingleton.h // CES #ifndef SynthesizeSingleton_h #define SynthesizeSingleton_h //聲明 #define DECLARE_SYNTHESIZE_SINGLETON_FOR_CLASS(classname) + (classname *)sharedInstance; //實現 #define IMPLEMENT_SYNTHESIZE_SINGLETON_FOR_CLASS(classname) static classname *shared##classname = nil; + (classname *)sharedInstance { @synchronized(self) { if (shared##classname == nil) { shared##classname = [[self alloc] init]; } } return shared##classname; } + (id)allocWithZone:(NSZone *)zone { @synchronized(self) { if (shared##classname == nil) { shared##classname = [super allocWithZone:zone]; return shared##classname; } } return nil; } - (id)copyWithZone:(NSZone *)zone { return self; }
使用方法:在你需要創建單例類的類的.h和.m文件中分別加入以下代碼(首先導入以上代碼所處的頭文件)
DECLARE_SYNTHESIZE_SINGLETON_FOR_CLASS(LoginManager)(.h)聲明 IMPLEMENT_SYNTHESIZE_SINGLETON_FOR_CLASS(LoginManager)(.m)實現