iOS之開發常用巨集

2021-07-22 20:51:29 字數 3869 閱讀 6389

//字串是否為空

#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開發常用巨集

字串是否為空 define kstringisempty str str iskindofclass nsnull class str nil str length 1 yes no 陣列是否為空 define karrayisempty array array nil array iskindof...

iOS開發常用巨集

大家都是知道使用巨集不僅方便,而且可以提高開發效率。下面總結了ios開發過程中的一些常用巨集,會持續的往裡面新增。字串是否為空 define kstringisempty str str iskindofclass nsnull class str nil str length 陣列是否為空 def...

iOS開發中常用的巨集

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