LogCategory.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // NSDictionary+LogCategory.m
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/3/9.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #import "LogCategory.h"
  9. #import <objc/runtime.h>
  10. @implementation NSArray (Log)
  11. #ifdef UseLogChinese
  12. - (NSString *)debugDescription {
  13. return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
  14. }
  15. - (NSString *)description {
  16. return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
  17. }
  18. - (NSString *)descriptionWithLocale:(id)locale {
  19. NSMutableString * string = [NSMutableString string];
  20. [string appendString:@"[\n"];
  21. NSUInteger count = self.count;
  22. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  23. NSString * temp = nil;
  24. if ([obj respondsToSelector:@selector(descriptionWithLocale:)]) {
  25. temp = [obj performSelector:@selector(descriptionWithLocale:) withObject:locale];
  26. temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
  27. } else {
  28. temp = [obj performSelector:@selector(description) withObject:nil];
  29. if ([obj isKindOfClass:[NSString class]]) {
  30. temp = [NSString stringWithFormat:@"\"%@\"", temp];
  31. }
  32. }
  33. [string appendFormat:@"\t%@", temp];
  34. if (idx+1 != count) {
  35. [string appendString:@","];
  36. }
  37. [string appendString:@"\n"];
  38. }];
  39. [string appendString:@"]"];
  40. return string;
  41. }
  42. #endif
  43. @end
  44. @implementation NSDictionary (Log)
  45. #ifdef UseLogChinese
  46. - (NSString *)debugDescription {
  47. return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
  48. }
  49. - (NSString *)description {
  50. return [NSString stringWithFormat:@"<%@ %p> %@", NSStringFromClass([self class]), self, [self descriptionWithLocale:nil]];
  51. }
  52. - (NSString *)descriptionWithLocale:(id)locale {
  53. NSMutableString * string = [NSMutableString string];
  54. [string appendString:@"{\n"];
  55. NSUInteger count = self.allKeys.count;
  56. for (id key in self.allKeys) {
  57. NSInteger index = [self.allKeys indexOfObject:key];
  58. id value = [self objectForKey:key];
  59. NSString * temp = nil;
  60. if ([value respondsToSelector:@selector(descriptionWithLocale:)]) {
  61. temp = [value performSelector:@selector(descriptionWithLocale:) withObject:locale];
  62. temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
  63. } else {
  64. temp = [value performSelector:@selector(description) withObject:nil];
  65. if ([value isKindOfClass:[NSString class]]) {
  66. temp = [NSString stringWithFormat:@"\"%@\"", temp];
  67. }
  68. }
  69. [string appendFormat:@"\t%@ = %@", key, temp];
  70. if (index+1 != count) {
  71. [string appendString:@";"];
  72. }
  73. [string appendString:@"\n"];
  74. }
  75. [string appendString:@"}"];
  76. return string;
  77. }
  78. #endif
  79. @end