宏定义记录重点
1.NSLog宏定义:
很多情况下我们会对这行log的所在的文件位置方法什么的会比较关心。在每次NSLog里都手动加上方法名字和位置信息什么的无疑是个笨办法,而如果一个工程里已经有很多NSLog
的调用了,一个一个手动去改的话无疑也是噩梦。我们通过宏,可以很简单地完成对NSLog
原生行为的改进,优雅,高效。只需要在预编译的pch文件中加上
//A better version of NSLog #define NSLog(format, ...) do { \ fprintf(stderr, "<%s : %d> %s\n", \ [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], \ __LINE__, __func__); \ (NSLog)((format), ##__VA_ARGS__); \ fprintf(stderr, "-------\n"); \ } while (0)
2.Rect, Size, Point:
#define NSLogRect(rect) NSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f", #rect, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height) #define NSLogSize(size) NSLog(@"%s w:%.4f, h:%.4f", #size, size.width, size.height) #define NSLogPoint(point) NSLog(@"%s x:%.4f, y:%.4f", #point, point.x, point.y)
3.以下是我项目中使用到的一些宏定义:
#ifndef Macros_Utils_h #define Macros_Utils_h // Thread #define MKAssertMainThread() NSAssert([NSThread isMainThread], @"这个方法必须在主线程调用"); #define MKAssertOtherThread() NSAssert(![NSThread isMainThread], @"这个方法必须在子线程调用"); // KVO KVC #define keyPath(obj, attr) @(((void)obj.attr, #attr)) #define keyPathWithSELName(sltName) NSStringFromSelector(@selector(sltName)) // property/selectorName -> keyPath // NSLog #if defined(DEBUG) || defined(_DEBUG) #define NSLog(format, ...) \ do {\ fprintf(stderr, "<%s : %d> %s\n", [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __func__); \ (NSLog)((format), ##__VA_ARGS__); \ fprintf(stderr, "-------\n"); \ } while (0) #else #define NSLog(...) #endif // iOS 版本 #define MKSystemVersionGreaterOrEqualThan(version) ([[[UIDevice currentDevice] systemVersion] floatValue] >= version) #define IOS7_OR_LATER MKSystemVersionGreaterOrEqualThan(7.0) #define IOS8_OR_LATER MKSystemVersionGreaterOrEqualThan(8.0) #define IOS9_OR_LATER MKSystemVersionGreaterOrEqualThan(9.0) #define IOS8_1_OR_LATER MKSystemVersionGreaterOrEqualThan(8.1) #define MKSystemVersionLowerOrEqualThan(version) ([[[UIDevice currentDevice] systemVersion] floatValue] < version) #define IOS6_EARLY MKSystemVersionLowerOrEqualThan(6.0) // safe access #define isNilOrNull(obj) (obj == nil || [obj isEqual:[NSNull null]]) #define isClassEqualNil(clazz) (clazz == Nil) #define NULL_POINTER NULL // bundle #define DOCUMENT_FOLDER ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]) #define bundlePathWithName(name) [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:name] #define bundleWithName(name) [NSBundle bundleWithPath:bundlePathWithName(name)] // 判断string是否为空 nil 或者 @""; #define IsNilString(__String) (__String==nil || [__String isEqualToString:@""] || [[__String stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]) #endif /* Macros_Utils_h */
暂时记录这么多,后续补充!