iOS中常見的一些宏

來源:互聯網
上載者:User

標籤:

原文連結

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或更高系統版本(該方法僅支援iOS10以下版本,謹慎使用,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)實現

 

iOS中常見的一些宏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.