Xcode8输出的日志中有一段奇怪的东西:
2016-09-20 13:12:11.855080 swiftChujian[6637:3736512] subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 13:12:11.861153 swiftChujian[6637:3736512] subsystem: com.apple.UIKit, category: HIDEventIncoming, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 13:12:11.880805 swiftChujian[6637:3736508] subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0
2016-09-20 13:12:11.917113 swiftChujian[6637:3736461] subsystem: com.apple.UIKit, category: StatusBar, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-20 13:12:11.986507 swiftChujian[6637:3736461] subsystem: com.apple.BackBoardServices.fence, category: App, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0
解决方案:
在Environment Variables中添加 OS_ACTIVITY_MODE 为 disable.
在运行,那些日志就没有了.
但是iOS 10真机调试的话,会同时屏蔽掉NSLog。如果NSLog宏定义是这样的话:
#define NSLog(format, ...) NSLog(format, ## __VA_ARGS__)
在完成OS_ACTIVITY_MODE设置后会发现真机调试时NSLog不输出了。
所以需要定义成另一个函数来输出,例如printf()。
NSLog宏定义的写法很多,这里贴上我的写法:
#ifdef DEBUG
#define NSLog(format, ...) printf("[%s] %s [第%d行] %s\n", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ## __VA_ARGS__] UTF8String]);
#else
#define NSLog(format, ...)
#endif
测试:
NSLog(@"初始化完毕");
NSLog(@"测试NSLog:%@", @"OK");
效果:
[19:47:57] -[ViewController viewDidLoad] [第26行] 初始化完毕
[19:47:57] -[ViewController TestLog] [第31行] 测试NSLog:OK