iOS 常用宏定义
ios 常用宏定义
//字符串是否为空
#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 kapplication [uiapplication sharedapplication]
#define kkeywindow [uiapplication sharedapplication].keywindow
#define kappdelegate [uiapplication sharedapplication].delegate
#define kuserdefaults [nsuserdefaults standarduserdefaults]
#define knotificationcenter [nsnotificationcenter defaultcenter]
//app版本号
#define kappversion [[[nsbundle mainbundle] infodictionary] objectforkey:@"cfbundleshortversionstring"]
//版本号
#define ksystemversion [[uidevice currentdevice] systemversion]
//获取当前语言
#define kcurrentlanguage ([[nslocale preferredlanguages] objectatindex:0])
//判断是否为iphone
#define kisiphone (ui_user_interface_idiom() == uiuserinterfaceidiomphone)
//判断是否为ipad
#define kisipad (ui_user_interface_idiom() == uiuserinterfaceidiompad)
//获取沙盒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),arc4random_uniform(256),arc4random_uniform(256))
#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)
下一篇: 趣味访客计数器设计两则