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