Sometimes, we need to make the code simple so that it's easy to read code. We can extract some unchanging stuff and change things as parameters. Defined as a macro, so it's much easier to write.
The following examples illustrate some common macro definitions and share them with you:
1. Determine if the device's operating system is IOS7
1. #define IOS7 ([[[Uidevice currentdevice].systemversion Doublevalue] >= 7.0])
2. Determine if the current device is IPhone5
1. #define KSCREENIPHONE5 ([[[UIScreen Mainscreen] bounds].size.height) >=568)
3. Get the height of the current screen
1. #define Kmainscreenheight ([UIScreen mainscreen].applicationframe.size.height)
4. Get the width of the current screen
1. #define KMAINSCREENWIDTH ([UIScreen mainscreen].applicationframe.size.width)
5. Get RGB color
1. #define Smscolor (R, G, b) [Uicolor colorwithred: (r)/255.0 Green: (g)/255.0 Blue: (b)/255.0 alpha:1.0]
6: Custom Log
#ifdef DEBUG
#define SMSLOG (...) NSLog (__va_args__)
#else
#define SMSLOG (...)
#endif
7. Single case
01.//@interface
#define SINGLETON_INTERFACE (ClassName)
03.+ (ClassName *) shared# #className;
04.
05.
06.//@implementation
#define Singleton_implementation (ClassName)
08.static ClassName *_instance;
09.+ (ID) allocwithzone: (struct _nszone *) zone
10.{
11.static dispatch_once_t Oncetoken;
12.dispatch_once (&oncetoken, ^{
13._instance = [Super Allocwithzone:zone];
14.});
15.return _instance;
16.}
17.+ (ClassName *) shared# #className
18.{
19.static dispatch_once_t Oncetoken;
20.dispatch_once (&oncetoken, ^{
21._instance = [[Self alloc] init];
22.});
23.return _instance;
24.}
iOS development--Definition of common macros