本文是接着上一篇博客iOS---学习研究大牛Git高星项目YYCategories(二)
大神果真是大神,一个简单的宏定义类,就有如此多的需要注意的地方。在一篇文章中写篇幅过长就分开写。
tip6:
/** Synthsize a weak or strong reference. */ #ifndef weakify #if DEBUG #if __has_feature(objc_arc) #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object; #else #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object; #endif #else #if __has_feature(objc_arc) #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object; #else #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object; #endif #endif #endif #ifndef strongify #if DEBUG #if __has_feature(objc_arc) #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object; #else #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object; #endif #else #if __has_feature(objc_arc) #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object; #else #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object; #endif #endif #endif
生成一个弱引用或者强引用。可以直接使用。
Example: @weakify(self) [self doSomething^{ @strongify(self) if (!self) return; ... }];
tip7:
/** Convert CFRange to NSRange @param range CFRange @return NSRange */ static inline NSRange YYNSRangeFromCFRange(CFRange range) { return NSMakeRange(range.location, range.length); } /** Convert NSRange to CFRange @param range NSRange @return CFRange */ static inline CFRange YYCFRangeFromNSRange(NSRange range) { return CFRangeMake(range.location, range.length); } /** Same as CFAutorelease(), compatible for iOS6 @param arg CFObject @return same as input */ static inline CFTypeRef YYCFAutorelease(CFTypeRef CF_RELEASES_ARGUMENT arg) { if (((long)CFAutorelease + 1) != 1) { return CFAutorelease(arg); } else { id __autoreleasing obj = CFBridgingRelease(arg); return (__bridge CFTypeRef)obj; } }
这只是一些转换。
tip8:
/** Profile time cost. @param block code to benchmark @param complete code time cost (millisecond) Usage: YYBenchmark(^{ // code }, ^(double ms) { NSLog("time cost: %.2f ms",ms); }); */ static inline void YYBenchmark(void (^block)(void), void (^complete)(double ms)) { // <QuartzCore/QuartzCore.h> version /* extern double CACurrentMediaTime (void); double begin, end, ms; begin = CACurrentMediaTime(); block(); end = CACurrentMediaTime(); ms = (end - begin) * 1000.0; complete(ms); */ // <sys/time.h> version struct timeval t0, t1; gettimeofday(&t0, NULL); block(); gettimeofday(&t1, NULL); double ms = (double)(t1.tv_sec - t0.tv_sec) * 1e3 + (double)(t1.tv_usec - t0.tv_usec) * 1e-3; complete(ms); }
这个函数还是挺实用的,可以用来计算block内代码的执行时间,通过该函数来测试写的代码执行效率.
返回值为毫秒,double类型.
tip9:
static inline NSDate *_YYCompileTime(const char *data, const char *time) { NSString *timeStr = [NSString stringWithFormat:@"%s %s",data,time]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MMM dd yyyy HH:mm:ss"]; [formatter setLocale:locale]; return [formatter dateFromString:timeStr]; } /** Get compile timestamp. @return A new date object set to the compile date and time. */ #ifndef YYCompileTime // use macro to avoid compile warning when use pch file #define YYCompileTime() _YYCompileTime(__DATE__, __TIME__) #endif
获取编译开始时间.格式为"MMM dd yyyy HH:mm:ss"
这两个方法的使用代码:
@weakify(self) YYBenchmark(^{ self.headView.clickHeadViewButtonTypeBlock = ^(ManagerHEadViewButtonType type) { if (type == ManagerHEadViewButtonLeft) { NSLog(@"标书归档"); @strongify(self) self.headView.backgroundColor = [UIColor redColor]; } else { NSLog(@"合同备案"); } }; }, ^(double ms) { NSLog(@"time cost: %.2f ms",ms); }); NSLog(@"%@", YYCompileTime());
结果显示: