Some of the common macros in iOS

Source: Internet
Author: User

1. Handling NSLog Events (developer mode printing, Publisher mode not printing)

#ifdef DEBUG #define NSLOG (FORMAT, ...) fprintf (stderr, "%s:%d\t%s\n", [[[NSString stringwithutf8string:__file__]  Lastpathcomponent] utf8string], __line__, [[NSString Stringwithformat:format, # #__VA_ARGS__] utf8string]); #else #define NSLOG (FORMAT, ...) nil #endif

2. Import some header files in OC language

#ifdef __objc__//import header file #endif

3. Handling Circular reference problems (processing the current class object)

#define WS (weakself) __weak __typeof (&*self) weakself = self;

4. Get the screen width high

#define SCREENWIDTH [[UIScreen mainscreen] bounds].size.width #define ScreenHeight [[UIScreen mainscreen] bounds]. Size.heigh

5. Judge IOS8 or higher system version (use cautiously, floatvalue is not reliable, for specific reasons see: HTTP://WWW.JIANSHU.COM/P/528897755DC8)

#define IOS8UP ([[Uidevice currentdevice].systemversion Floatvalue] >= 8)

6. Set the color RGB value

#define RGB (a,b,c) [Uicolor colorwithred: (a/255.0) Green: (b/255.0) Blue: (c/255.0) alpha:1.0]

7. Set the color RGB value + transparency

#define RGBA (a,b,c,d) [Uicolor colorwithred: (a/255.0) Green: (b/255.0) Blue: (c/255.0) alpha:d]

8. Support Horizontal Screen

  #if  __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 //  current Xcode support iOS8 and above  # define screen_width  ([[Uiscreen mainscreen] respondstoselector: @selector (nativeBounds)]?[ uiscreen mainscreen].nativebounds.size.width/[uiscreen mainscreen].nativescale:[uiscreen  Mainscreen].bounds.size.width)   #define  SCREENH_HEIGHT  ([[uiscreen mainscreen]  Respondstoselector: @selector (nativebounds)]? [uiscreen mainscreen].nativebounds.size.height/[uiscreen mainscreen].nativescale:[uiscreen  Mainscreen].bounds.size.height)   #define  SCREEN_SIZE  ([[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)   #else   #define  screen_width [uiscreen mainscreen].bounds.size.width  #define  screenh_height [uiscreen mainscreen]. bounds.size.height  #define  SCREEN_SIZE [UIScreen mainScreen].bounds.size  #endif

9. Set Random Colors

#define Lrrandomcolor [Uicolor colorwithred:arc4random_uniform (/255.0 green:arc4random_uniform)/255.0 Blue: Arc4random_uniform (/255.0 alpha:1.0]

10. Set the rounded border of the view

#define Lrviewborderradius (View, Radius, Width, Color) \[view.layer Setcornerradius: (RADIUS)]; [View.layer Setmaskstobounds:yes]; [View.layer setborderwidth: (Width)]; [View.layer Setbordercolor:[color Cgcolor]]

11. Get Picture Resources

#define KGETIMAGE (imageName) [UIImage imagenamed:[nsstring stringwithformat:@ "%@", ImageName]]

12. Get the current language

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

13. Determine the current iphone device/system version

Determine if it is iphone#define is_iphone  (Ui_user_interface_idiom ()  == uiuserinterfaceidiomphone)// Determine if it is ipad#define is_ipad  (Ui_user_interface_idiom ()  == uiuserinterfaceidiompad)//Whether it is an ipod #define  IS_IPOD  ([[[uidevice currentdevice] model] isequaltostring:@] Ipod touch " ])//  to determine whether it is  iphone 5se#define iphone5se [[uiscreen mainscreen] bounds]. size.width == 320.0f && [[uiscreen mainscreen] bounds].size.height  == 568.0f//  Judging whether it is iphone 6/6s#define iphone6_6s [[uiscreen mainscreen]  Bounds].size.width == 375.0f && [[uiscreen mainscreen] bounds]. size.height == 667.0f//  Judging whether it is iphone 6plus/6splus#define iphone6plus_6splus [[ uiscreen mainscreen] bounds].size.width == 414.0f && [[uiscreen  Mainscreen] bounds].size.height == 736.0f//getting the system version #define ios_system_version [[[uidevice currentdevice]  Systemversion] floatvalue]

14. Judging whether it is a real machine or a simulator

#if target_os_iphone//iphone device#endif#if Target_iphone_simulator//iphone simulator#endif

15. Sandbox catalog File

Get Temp#define kpathtemp nstemporarydirectory ()//Get sandbox document#define kpathdocument [ Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject]//get sandbox Cache#define Kpathcache [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) FirstObject]

16. Use of macros and const

Many small partners define a constant string, which is defined as a macro, the most typical example being the server's address. In this case, all the small partners that define constant characters with a macro are defined with a const! Why is it? Let's see:

Macro usage: General string extraction macro, code to use macros.
Const usage: Commonly used strings are defined as const (for constant strings Apple recommends that we use const).
Differences between macros and const:

1. The compile time is different, the macro is precompiled, the const belongs to the compile time

2. Macro can define the code, const can not, multiple macros for the compilation will be relatively long, the impact of development efficiency, debugging too slow, const will only compile once, shorten the compilation time.

3. Macros do not check for errors, const will check for errors

In comparison with the above, we later in the development if the definition of a constant string is const, define the code with a macro.

Static NSString * Const LOGINACCOUNT = @ "Loginaccount"; static NSString * Const LOGINPASSWORD = @ "Loginpassword";

17. Single-Instance a class

  SynthesizeSingleton.h//  CES#ifndef SynthesizeSingleton_h#define  synthesizesingleton_h //Statement #define declare_synthesize_singleton_for_class (classname)  +  ( classname *) sharedinstance; //Implementation #define implement_synthesize_singleton_for_class (classname)   static classname *shared# #classname  = nil; +  (classname *) sharedinstance  {      @synchronized (self)   {  if  ( shared# #classname  == nil)   {   shared# #classname  = [[self  alloc] init];  }  }  return shared# #classname;  }    +  (ID) Allocwithzone: (nszone *) zone  {   @synchronized (self)    { if  (shared# #classname  == nil)   {  shared# #classname  =  [super allocwithzone:zone];  return shared# #classname;   }  }   return nil;  }   -  (ID) Copywithzone: (nszone *) zone {   return self; }

How to use: Add the following code in the. h and. m files of the class where you need to create the Singleton class (first import the header file where the above code is used)

Declare_synthesize_singleton_for_class (Loginmanager) (. h) Declares Implement_synthesize_singleton_for_class ( Loginmanager) (. m) Implementation



Some of the common macros in iOS

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.