IOS開發中常用的宏定義

來源:互聯網
上載者:User

標籤:自訂log   判斷當前裝置   宏定義單例   singleton   

有些時候,我們需要將代碼簡潔化,這樣便於讀代碼。我們可以將一些不變的東東抽取出來,將變化的東西作為參數。定義為宏,這樣在寫的時候就簡單多了。


下面例舉了一些常用的宏定義和大家分享:

1. 判斷裝置的作業系統是不是ios7

#define IOS7   (  [[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] )

2. 判斷當前裝置是不是iPhone5 

#define kScreenIphone5    (([[UIScreen mainScreen] bounds].size.height)>=568)




3.擷取當前螢幕的高度
#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)

4.擷取當前螢幕的寬度

#define kMainScreenWidth  ([UIScreen mainScreen].applicationFrame.size.width)

5.獲得RGB顏色

#define SMSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]


6..自訂Log

#ifdef DEBUG

#define SMSLog(...) NSLog(__VA_ARGS__)

#else

#define SMSLog(...)

#endif


7.單例

// @interface#define singleton_interface(className) + (className *)shared##className;// @implementation#define singleton_implementation(className) static className *_instance; + (id)allocWithZone:(struct _NSZone *)zone {     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         _instance = [super allocWithZone:zone];     });     return _instance; } + (className *)shared##className {     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         _instance = [[self alloc] init];     });     return _instance; }




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.