iOS开发中那些高效常用的宏

    1. #ifndef MacroDefinition_h  
    2. #define MacroDefinition_h  
    3.   
    4. //-------------------获取设备大小-------------------------  
    5. //NavBar高度  
    6. #define NavigationBar_HEIGHT 44  
    7. //获取屏幕 宽度、高度  
    8. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)  
    9. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)  
    10.   
    11. //-------------------获取设备大小-------------------------  
    12.   
    13.   
    14. //-------------------打印日志-------------------------  
    15. //DEBUG  模式下打印日志,当前行  
    16. #ifdef DEBUG  
    17. #   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);  
    18. #else  
    19. #   define DLog(...)  
    20. #endif  
    21.   
    22.   
    23. //重写NSLog,Debug模式下打印日志和当前行数  
    24. #if DEBUG  
    25. #define NSLog(FORMAT, ...) fprintf(stderr," function:%s line:%d content:%s ", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);  
    26. #else  
    27. #define NSLog(FORMAT, ...) nil  
    28. #endif  
    29.   
    30. //DEBUG  模式下打印日志,当前行 并弹出一个警告  
    31. #ifdef DEBUG  
    32. #   define ULog(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s  [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }  
    33. #else  
    34. #   define ULog(...)  
    35. #endif  
    36.   
    37.   
    38. #define ITTDEBUG  
    39. #define ITTLOGLEVEL_INFO     10  
    40. #define ITTLOGLEVEL_WARNING  3  
    41. #define ITTLOGLEVEL_ERROR    1  
    42.   
    43. #ifndef ITTMAXLOGLEVEL  
    44.   
    45. #ifdef DEBUG  
    46. #define ITTMAXLOGLEVEL ITTLOGLEVEL_INFO  
    47. #else  
    48. #define ITTMAXLOGLEVEL ITTLOGLEVEL_ERROR  
    49. #endif  
    50.   
    51. #endif  
    52.   
    53. // The general purpose logger. This ignores logging levels.  
    54. #ifdef ITTDEBUG  
    55. #define ITTDPRINT(xx, ...)  NSLog(@"%s(%d): " xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)  
    56. #else  
    57. #define ITTDPRINT(xx, ...)  ((void)0)  
    58. #endif  
    59.   
    60. // Prints the current method's name.  
    61. #define ITTDPRINTMETHODNAME() ITTDPRINT(@"%s", __PRETTY_FUNCTION__)  
    62.   
    63. // Log-level based logging macros.  
    64. #if ITTLOGLEVEL_ERROR <= ITTMAXLOGLEVEL  
    65. #define ITTDERROR(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)  
    66. #else  
    67. #define ITTDERROR(xx, ...)  ((void)0)  
    68. #endif  
    69.   
    70. #if ITTLOGLEVEL_WARNING <= ITTMAXLOGLEVEL  
    71. #define ITTDWARNING(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)  
    72. #else  
    73. #define ITTDWARNING(xx, ...)  ((void)0)  
    74. #endif  
    75.   
    76. #if ITTLOGLEVEL_INFO <= ITTMAXLOGLEVEL  
    77. #define ITTDINFO(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)  
    78. #else  
    79. #define ITTDINFO(xx, ...)  ((void)0)  
    80. #endif  
    81.   
    82. #ifdef ITTDEBUG  
    83. #define ITTDCONDITIONLOG(condition, xx, ...) { if ((condition)) {   
    84. ITTDPRINT(xx, ##__VA_ARGS__);   
    85. }   
    86. } ((void)0)  
    87. #else  
    88. #define ITTDCONDITIONLOG(condition, xx, ...) ((void)0)  
    89. #endif  
    90.   
    91. #define ITTAssert(condition, ...)                                         
    92. do {                                                                        
    93. if (!(condition)) {                                                       
    94. [[NSAssertionHandler currentHandler]                                    
    95. handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]   
    96. file:[NSString stringWithUTF8String:__FILE__]    
    97. lineNumber:__LINE__                                    
    98. description:__VA_ARGS__];                               
    99. }                                                                         
    100. while(0)  
    101.   
    102. //---------------------打印日志--------------------------  
    103.   
    104.   
    105. //----------------------系统----------------------------  
    106.   
    107. //获取系统版本  
    108. #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  
    109. #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]  
    110.   
    111. //获取当前语言  
    112. #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])  
    113.   
    114. //判断是否 Retina屏、设备是否%fhone 5、是否是iPad  
    115. #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)  
    116. #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)  
    117. #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)  
    118.   
    119.   
    120. //判断是真机还是模拟器  
    121. #if TARGET_OS_IPHONE  
    122. //iPhone Device  
    123. #endif  
    124.   
    125. #if TARGET_IPHONE_SIMULATOR  
    126. //iPhone Simulator  
    127. #endif  
    128.   
    129. //检查系统版本  
    130. #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)  
    131. #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)  
    132. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)  
    133. #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)  
    134. #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)  
    135.   
    136.   
    137. //----------------------系统----------------------------  
    138.   
    139.   
    140. //----------------------内存----------------------------  
    141.   
    142. //使用ARC和不使用ARC  
    143. #if __has_feature(objc_arc)  
    144. //compiling with ARC  
    145. #else  
    146. // compiling without ARC  
    147. #endif  
    148.   
    149. #pragma mark - common functions  
    150. #define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }  
    151.   
    152. //释放一个对象  
    153. #define SAFE_DELETE(P) if(P) { [P release], P = nil; }  
    154.   
    155. #define SAFE_RELEASE(x) [x release];x=nil  
    156.   
    157.   
    158.   
    159. //----------------------内存----------------------------  
    160.   
    161.   
    162. //----------------------图片----------------------------  
    163.   
    164. //读取本地图片  
    165. #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]  
    166.   
    167. //定义UIImage对象  
    168. #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]  
    169.   
    170. //定义UIImage对象  
    171. #define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]]  
    172.   
    173. //建议使用前两种宏定义,性能高于后者  
    174. //----------------------图片----------------------------  
    175.   
    176.   
    177.   
    178. //----------------------颜色类---------------------------  
    179. // rgb颜色转换(16进制->10进制)  
    180. #define UIColorFromRGB(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]  
    181.   
    182. //带有RGBA的颜色设置  
    183. #define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]  
    184.   
    185. // 获取RGB颜色  
    186. #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]  
    187. #define RGB(r,g,b) RGBA(r,g,b,1.0f)  
    188.   
    189. //背景色  
    190. #define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]  
    191.   
    192. //清除背景色  
    193. #define CLEARCOLOR [UIColor clearColor]  
    194.   
    195. #pragma mark - color functions  
    196. #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]  
    197. #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]  
    198.   
    199. //----------------------颜色类--------------------------  
    200.   
    201.   
    202.   
    203. //----------------------其他----------------------------  
    204.   
    205. //方正黑体简体字体定义  
    206. #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]  
    207.   
    208.   
    209. //定义一个API  
    210. #define APIURL                @"http://xxxxx/"  
    211. //登陆API  
    212. #define APILogin              [APIURL stringByAppendingString:@"Login"]  
    213.   
    214. //设置View的tag属性  
    215. #define VIEWWITHTAG(_OBJECT, _TAG)    [_OBJECT viewWithTag : _TAG]  
    216. //程序的本地化,引用国际化的文件  
    217. #define MyLocal(x, ...) NSLocalizedString(x, nil)  
    218.   
    219. //G-C-D  
    220. #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)  
    221. #define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)  
    222.   
    223. //NSUserDefaults 实例化  
    224. #define USER_DEFAULT [NSUserDefaults standardUserDefaults]  
    225.   
    226.   
    227. //由角度获取弧度 有弧度获取角度  
    228. #define degreesToRadian(x) (M_PI * (x) / 180.0)  
    229. #define radianToDegrees(radian) (radian*180.0)/(M_PI)  
    230.   
    231.   
    232.   
    233. //单例化一个类  
    234. #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname)   
    235.   
    236. static classname *shared##classname = nil;   
    237.   
    238. + (classname *)shared##classname   
    239. {   
    240. @synchronized(self)   
    241. {   
    242. if (shared##classname == nil)   
    243. {   
    244. shared##classname = [[self alloc] init];   
    245. }   
    246. }   
    247.   
    248. return shared##classname;   
    249. }   
    250.   
    251. + (id)allocWithZone:(NSZone *)zone   
    252. {   
    253. @synchronized(self)   
    254. {   
    255. if (shared##classname == nil)   
    256. {   
    257. shared##classname = [super allocWithZone:zone];   
    258. return shared##classname;   
    259. }   
    260. }   
    261.   
    262. return nil;   
    263. }   
    264.   
    265. - (id)copyWithZone:(NSZone *)zone   
    266. {   
    267. return self;   
    268. }  
    269.   
    270.   
    271.   
    272. #endif 
原文地址:https://www.cnblogs.com/er-dai-ma-nong/p/4935432.html