You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1 KiB

  1. //
  2. // FLEXObjectRef.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/24/18.
  6. // Copyright (c) 2018 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXObjectRef.h"
  9. #import <objc/runtime.h>
  10. @implementation FLEXObjectRef
  11. + (instancetype)referencing:(id)object {
  12. return [[self alloc] initWithObject:object ivarName:nil];
  13. }
  14. + (instancetype)referencing:(id)object ivar:(NSString *)ivarName {
  15. return [[self alloc] initWithObject:object ivarName:ivarName];
  16. }
  17. + (NSArray<FLEXObjectRef *> *)referencingAll:(NSArray *)objects {
  18. NSMutableArray<FLEXObjectRef *> *refs = [NSMutableArray array];
  19. for (id obj in objects) {
  20. [refs addObject:[self referencing:obj]];
  21. }
  22. return refs;
  23. }
  24. - (id)initWithObject:(id)object ivarName:(NSString *)ivar {
  25. self = [super init];
  26. if (self) {
  27. _object = object;
  28. NSString *class = NSStringFromClass(object_getClass(object));
  29. if (ivar) {
  30. _reference = [NSString stringWithFormat:@"%@ %@", class, ivar];
  31. } else {
  32. _reference = [NSString stringWithFormat:@"%@ %p", class, object];
  33. }
  34. }
  35. return self;
  36. }
  37. @end