iOS開發中常用到的巨集定義

2021-07-24 16:52:10 字數 3869 閱讀 8917

//字串是否為空

#define kstringisempty(str) ([str iskindofclass:[nsnull class]] || str == nil || [str length] < 1 ? yes : no )

//陣列是否為空

#define karrayisempty(array) (array == nil || [array iskindofclass:[nsnull class]] || array.count == 0)

//字典是否為空

#define kdictisempty(dic) (dic == nil || [dic iskindofclass:[nsnull class]] || dic.allkeys == 0)

//是否是空物件

#define kobjectisempty(_object) (_object == nil \

|| [_object iskindofclass:[nsnull class]] \

|| ([_object respondstoselector:@selector(length)] && [(nsdata *)_object length] == 0) \

|| ([_object respondstoselector:@selector(count)] && [(nsarray *)_object count] == 0))

//獲取螢幕寬度與高度

#define kscreenwidth \

([[uiscreen mainscreen] respondstoselector:@selector(nativebounds)] ? [uiscreen mainscreen].nativebounds.size.width/[uiscreen mainscreen].nativescale : [uiscreen mainscreen].bounds.size.width)

#define kscreenheight \

([[uiscreen mainscreen] respondstoselector:@selector(nativebounds)] ? [uiscreen mainscreen].nativebounds.size.height/[uiscreen mainscreen].nativescale : [uiscreen mainscreen].bounds.size.height)

#define kscreensize \

([[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)

//一些縮寫

#define kuserdefaults [nsuserdefaults standarduserdefaults]

#define knotificationcenter [nsnotificationcenter defaultcenter]

//系統版本號

#define ksystemversion [[uidevice currentdevice] systemversion]

//獲取當前語言

#define kcurrentlanguage ([[nslocale preferredlanguages] objectatindex:0])

//判斷是否為iphone

#define kisiphone (ui_user_inte***ce_idiom() == uiuserinte***ceidiomphone)

//判斷是否為ipad

#define kisipad (ui_user_inte***ce_idiom() == uiuserinte***ceidiompad)

//獲取沙盒document路徑

#define kdocumentpath [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) firstobject]

//獲取沙盒temp路徑

#define ktemppath nstemporarydirectory()

//獲取沙盒cache路徑

#define kcachepath [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) firstobject]

//判斷是真機還是模擬器

#if target_os_iphone

//真機

#endif

#if target_iphone_simulator

//模擬器

#endif

//開發的時候列印,但是發布的時候不列印的nslog

#ifdef debug

#define nslog(...) nslog(@"%s 第%d行 \n %@\n\n",__func__,__line__,[nsstring stringwithformat:__va_args__])

#else

#define nslog(...)

#endif

//顏色

#define krgbcolor(r, g, b) [uicolor colorwithred:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

#define krgbacolor(r, g, b, a) [uicolor colorwithred:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]

#define krandomcolor krgbcolor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0)

#define kcolorwithhex(rgbvalue) \

[uicolor colorwithred:((float)((rgbvalue & 0xff0000) >> 16)) / 255.0 \

green:((float)((rgbvalue & 0xff00) >> 8)) / 255.0 \

blue:((float)(rgbvalue & 0xff)) / 255.0 alpha:1.0]

//弱引用/強引用

#define kweakself(type) __weak typeof(type) weak##type = type;

#define kstrongself(type) __strong typeof(type) type = weak##type;

//由角度轉換弧度

#define kdegreestoradian(x) (m_pi * (x) / 180.0)

//由弧度轉換角度

#define kradiantodegrees(radian) (radian * 180.0) / (m_pi)

//獲取一段時間間隔

#define kstarttime cfabsolutetime start = cfabsolutetimegetcurrent();

#define kendtime nslog(@"time: %f", cfabsolutetimegetcurrent() - start)

iOS開發中常用的巨集

ui尺寸類的巨集 log日誌列印 判斷裝置?系統?判斷當前的iphone裝置 系統版本 定義乙個define函式 define tt release cf safely ref if has feature objc arc compiling with arc else compiling wit...

iOS開發中常用到的加密方式

1 base64 1.1 簡介 base64編碼的思想是是採用64個基本的ascii碼字元對資料進行重新編碼。它將需要編碼的資料拆分成位元組陣列。以3個位元組為一組。按順序排列24位資料,再把這24位資料分成4組,即每組6位。再在每組的的最高位前補兩個0湊足乙個位元組。這樣就把乙個3位元組為一組的資...

iOS開發中常用到的加密方式

1 base64 1.1 簡介 base64編碼的思想是是採用64個基本的ascii碼字元對資料進行重新編碼。它將需要編碼的資料拆分成位元組陣列。以3個位元組為一組。按順序排列24位資料,再把這24位資料分成4組,即每組6位。再在每組的的最高位前補兩個0湊足乙個位元組。這樣就把乙個3位元組為一組的資...