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
View Sourceprint?
1.
#define IOS7 ( [[[UIDevice currentDevice].systemVersion doubleValue] >=
7.0
] )
2. Determine if the current device is IPhone5
View Sourceprint?
1.
#define kScreenIphone5 (([[UIScreen mainScreen] bounds].size.height)>=
568
)
3. Get the height of the current screen
View Sourceprint?
1.
#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
4. Get the width of the current screen
View Sourceprint?
1.
#define kMainScreenWidth ([UIScreen mainScreen].applicationFrame.size.width)
5. Get RGB color
View Sourceprint?
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
View Sourceprint?
01.
// @interface
02.
#define singleton_interface(className)
03.
+ (className *)shared##className;
04.
05.
06.
// @implementation
07.
#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.
}
Definitions of common macros for iOS development