1.處理NSLog事件(開發者模式打印,發佈者模式不打印)html
1ios 2服務器 3dom 4spa 5調試 |
#ifdef DEBUG code #define NSLog(FORMAT, ...) fprintf (stderr, "%s:%d\t%s\n" ,[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]); orm # else htm #define NSLog(FORMAT, ...) nil 對象 #endif |
2.在OC語言的狀況下導入某些頭文件
1 2 3 |
#ifdef __OBJC__ //導入頭文件 #endif |
3.處理循環引用問題(處理當前類對象)
1 |
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self; |
4.獲取屏幕寬高
1 2 |
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width #define ScreenHeight [[UIScreen mainScreen] bounds].size.heigh |
5.判斷iOS8或更高系統版本(謹慎使用,floatValue是不靠譜的,具體緣由請看:http://www.jianshu.com/p/528897755dc8)
1 |
#define IOS8UP ([[UIDevice currentDevice].systemVersion floatValue] >= 8) |
6.設置顏色RGB值
1 |
#define RGB(a,b,c) [UIColor colorWithRed:(a/255.0) green:(b/255.0) blue:(c/255.0) alpha:1.0] |
7.設置顏色RGB值+透明度
1 |
#define RGBA(a,b,c,d) [UIColor colorWithRed:(a/255.0) green:(b/255.0) blue:(c/255.0) alpha:d] |
8.支持橫屏
1 2 3 4 5 6 7 8 9 |
# 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.設置隨機顏色
1 |
#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的圓角邊框
1 2 3 4 5 |
#define LRViewBorderRadius(View, Radius, Width, Color)\\ [View.layer setCornerRadius:(Radius)];\ [View.layer setMasksToBounds:YES];\ [View.layer setBorderWidth:(Width)];\ [View.layer setBorderColor:[Color CGColor]] |
11.獲取圖片資源
1 |
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@ "%@" ,imageName]] |
12.獲取當前語言
1 |
#define LRCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0]) |
13.判斷當前的iPhone設備/系統版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//判斷是否爲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.判斷是真機仍是模擬器
1 |
#if TARGET_OS_IPHONE//iPhone Device#endif#if TARGET_IPHONE_SIMULATOR//iPhone Simulator#endif |
15.沙盒目錄文件
1 2 3 4 5 6 |
//獲取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,定義代碼就用宏。
1 |
static NSString * const loginAccount = @ "loginAccount" ; static NSString * const loginPassword = @ "loginPassword" ; |
17.單例化一個類
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
// // 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文件中分別加入如下代碼(首先導入以上代碼所處的頭文件)
1 |
DECLARE_SYNTHESIZE_SINGLETON_FOR_CLASS(LoginManager)(.h)聲明 IMPLEMENT_SYNTHESIZE_SINGLETON_FOR_CLASS(LoginManager)(.m)實現 |
http://www.cocoachina.com/ios/20160908/17510.html
by_cocoachina