IOS Development Common macros

Source: Internet
Author: User

Source: Ios_ Komatsu

Links: http://www.jianshu.com/p/be00c3f3cafd

We all know that using macros is not only convenient, but also can improve development efficiency. The following summarizes some of the commonly used macros in the iOS development process, which will continue to be added.

Whether the string is empty

#define KSTRINGISEMPTY (str) ([str iskindofclass:[nsnull class]] | | str = = NIL | | [STR length] < 1? Yes:no)

Whether the array is empty

#define KARRAYISEMPTY (array) (array = = Nil | | [Array Iskindofclass:[nsnull class] | | Array.count = = 0)

Whether the dictionary is empty

#define KDICTISEMPTY (DIC) (dic = = Nil | | [dic Iskindofclass:[nsnull class] | | Dic.allkeys = = 0)

Whether it is an empty object

#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))

Get screen width and height

#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 )

Some abbreviations

#define Kapplication [UIApplication sharedapplication]

#define Kkeywindow [UIApplication Sharedapplication].keywindow

#define Kappdelegate [UIApplication sharedapplication].delegate

#define KUSERDEFAULTS [Nsuserdefaults Standarduserdefaults]

#define Knotificationcenter [Nsnotificationcenter Defaultcenter]

App version number

#define Kappversion [[[NSBundle Mainbundle] infodictionary] objectforkey:@ "cfbundleshortversionstring"]

System version number

#define Ksystemversion [[Uidevice Currentdevice] systemversion]

Get the current language

#define KCURRENTLANGUAGE ([[Nslocale preferredlanguages] objectatindex:0])

Determine if the iphone

#define Kisiphone (ui_user_interface_idiom () = = Uiuserinterfaceidiomphone)

Determine if the ipad

#define KISIPAD (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)

Get the Sandbox document path

#define Kdocumentpath [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) FirstObject]

Get Sandbox temp Path

#define Ktemppath nstemporarydirectory ()

Get Sandbox cache Path

#define Kcachepath [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) FirstObject]

Judging whether it's a real machine or a simulator

#if Target_os_iphone

Real Machine

#endif

#if Target_iphone_simulator

Analog device

#endif

Print when developing, but not print when publishing NSLog

#ifdef DEBUG

#define NSLOG (...) NSLog (@ "%s line%d \%@\n\n", __func__,__line__,[nsstring stringwithformat:__va_args__])

#else

#define NSLOG (...)

#endif

Color

#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 (/255.0,arc4random_uniform)/255.0,arc4random_uniform ( 256)/255.0)

#define KCOLORWITHHEX (Rgbvalue) \

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

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

Blue:((float) (rgbvalue & 0xFF)) / 255.0 Alpha: 1.0]

Weak references/strong references

#define KWEAKSELF (Type) __weak typeof (Type) weak# #type = type;

#define KSTRONGSELF (Type) __strong typeof (type) type = weak# #type;

Convert radians from angle

#define Kdegreestoradian (x) (M_PI * (x)/180.0)

Convert angle from radians

#define Kradiantodegrees (Radian) (Radian * 180.0)/(M_PI)

Get a time interval

#define Kstarttime Cfabsolutetime start = Cfabsolutetimegetcurrent ();

#define Kendtime NSLog (@ "Time:%f", Cfabsolutetimegetcurrent ()-start)

Keep updating ....

IOS Development Common macros

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.