LookinObject.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LookinObject.m
  4. // Lookin
  5. //
  6. // Created by Li Kai on 2019/4/20.
  7. // https://lookin.work
  8. //
  9. #import "LookinObject.h"
  10. #import "LookinIvarTrace.h"
  11. #import "NSArray+Lookin.h"
  12. #import "NSString+Lookin.h"
  13. #if TARGET_OS_IPHONE
  14. #import "NSObject+LookinServer.h"
  15. #endif
  16. @implementation LookinObject
  17. #if TARGET_OS_IPHONE
  18. + (instancetype)instanceWithObject:(NSObject *)object {
  19. LookinObject *lookinObj = [LookinObject new];
  20. lookinObj.oid = [object lks_registerOid];
  21. lookinObj.memoryAddress = [NSString stringWithFormat:@"%p", object];
  22. lookinObj.classChainList = [object lks_classChainListWithSwiftPrefix:YES];
  23. lookinObj.specialTrace = object.lks_specialTrace;
  24. lookinObj.ivarTraces = object.lks_ivarTraces;
  25. return lookinObj;
  26. }
  27. #endif
  28. #pragma mark - <NSCopying>
  29. - (id)copyWithZone:(NSZone *)zone {
  30. LookinObject *newObject = [[LookinObject allocWithZone:zone] init];
  31. newObject.oid = self.oid;
  32. newObject.memoryAddress = self.memoryAddress;
  33. newObject.classChainList = self.classChainList;
  34. newObject.specialTrace = self.specialTrace;
  35. newObject.ivarTraces = [self.ivarTraces lookin_map:^id(NSUInteger idx, LookinIvarTrace *value) {
  36. return value.copy;
  37. }];
  38. return newObject;
  39. }
  40. #pragma mark - <NSSecureCoding>
  41. - (void)encodeWithCoder:(NSCoder *)aCoder {
  42. [aCoder encodeObject:@(self.oid) forKey:@"oid"];
  43. [aCoder encodeObject:self.memoryAddress forKey:@"memoryAddress"];
  44. [aCoder encodeObject:self.classChainList forKey:@"classChainList"];
  45. [aCoder encodeObject:self.specialTrace forKey:@"specialTrace"];
  46. [aCoder encodeObject:self.ivarTraces forKey:@"ivarTraces"];
  47. }
  48. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  49. if (self = [super init]) {
  50. self.oid = [(NSNumber *)[aDecoder decodeObjectForKey:@"oid"] unsignedLongValue];
  51. self.memoryAddress = [aDecoder decodeObjectForKey:@"memoryAddress"];
  52. self.classChainList = [aDecoder decodeObjectForKey:@"classChainList"];
  53. self.specialTrace = [aDecoder decodeObjectForKey:@"specialTrace"];
  54. self.ivarTraces = [aDecoder decodeObjectForKey:@"ivarTraces"];
  55. }
  56. return self;
  57. }
  58. - (void)setClassChainList:(NSArray<NSString *> *)classChainList {
  59. _classChainList = classChainList;
  60. }
  61. + (BOOL)supportsSecureCoding {
  62. return YES;
  63. }
  64. - (NSString *)completedSelfClassName {
  65. return self.classChainList.firstObject;
  66. }
  67. - (NSString *)shortSelfClassName {
  68. return [[self completedSelfClassName] lookin_shortClassNameString];
  69. }
  70. @end
  71. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */