前言
升级Xcode 8
之后运行项目,会打印一些烦人的Log
信息,解决的办法就是OS_ACTIVITY_MODE = disable
,具体请移步:兼容iOS 10 资料整理笔记。
这种办法确实解决了我们的问题。但是又出现的一个问题就是在iOS 10
模拟器上是正常的,可是在iOS 10
真机测试所有的Log
日志全部被屏蔽了!大家误以为是之前的设置导致这种问题的出现,其实不然。这个问题应该是iOS 10
开始为了在真机上提高性能,所以把Log
日志给屏蔽了。
自定义Log
对我们来说真机测试也是离不开Log
日志的,那我们就来解决这个问题。首先我们最初自定义的Log
日志是这样的:
1 2 3 4 5 6 7 |
#ifdef DEBUG #define LRString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent #define LRLog(...) NSLog(@"%@ 第%d行 \n %@\n\n",LRString,__LINE__,[NSString stringWithFormat:__VA_ARGS__]) #else #define LRLog(...) #endif |
系统的NSLog()
已经不好使了,这个只能在iOS 9
之前的系统管用,如果想要在iOS 10
系统的手机也能打印日志,我们需要用到printf()
如下:
1 2 3 4 5 6 7 |
#ifdef DEBUG #define LRString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent #define LRLog(...) printf("%s: %s 第%d行: %s\n\n",[[NSString lr_stringDate] UTF8String], [LRString UTF8String] ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]); #else #define LRLog(...) #endif |
再看看我在真机测试上面打印的Log
日志:
1.[[NSString lr_stringDate] UTF8String]
是打印的时间,如果不喜欢打印这个时间可以去掉:
1 2 3 4 5 6 |
+ (NSString *)lr_stringDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:[NSDate date]]; return dateString; } |
2.使用UTF8String
的原因就是printf
是C
语言的,所以需要通过这个方法转换一下才能打印。
以上就是解决的办法,并且我也在
Debug
模式下和Release
模式下分别测试运行是没有问题的,如果有些地方理解错误还希望有人帮忙指正。