FLEXInstancesTableViewController.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // FLEXInstancesTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/28/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXInstancesTableViewController.h"
  9. #import "FLEXObjectExplorerFactory.h"
  10. #import "FLEXObjectExplorerViewController.h"
  11. #import "FLEXRuntimeUtility.h"
  12. #import "FLEXUtility.h"
  13. #import "FLEXHeapEnumerator.h"
  14. @interface FLEXInstancesTableViewController ()
  15. @end
  16. @implementation FLEXInstancesTableViewController
  17. + (instancetype)instancesTableViewControllerForClassName:(NSString *)className
  18. {
  19. const char *classNameCString = [className UTF8String];
  20. NSMutableArray *instances = [NSMutableArray array];
  21. [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id object, __unsafe_unretained Class actualClass) {
  22. if (strcmp(classNameCString, class_getName(actualClass)) == 0) {
  23. // Note: objects of certain classes crash when retain is called. It is up to the user to avoid tapping into instance lists for these classes.
  24. // Ex. OS_dispatch_queue_specific_queue
  25. // In the future, we could provide some kind of warning for classes that are known to be problematic.
  26. [instances addObject:object];
  27. }
  28. }];
  29. FLEXInstancesTableViewController *instancesViewController = [[self alloc] init];
  30. instancesViewController.instances = instances;
  31. instancesViewController.title = [NSString stringWithFormat:@"%@ (%lu)", className, (unsigned long)[instances count]];
  32. return instancesViewController;
  33. }
  34. #pragma mark - Table View Data Source
  35. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  36. {
  37. return 1;
  38. }
  39. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  40. {
  41. return [self.instances count];
  42. }
  43. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  44. {
  45. static NSString *CellIdentifier = @"Cell";
  46. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  47. if (!cell) {
  48. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  49. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  50. UIFont *cellFont = [FLEXUtility defaultTableViewCellLabelFont];
  51. cell.textLabel.font = cellFont;
  52. cell.detailTextLabel.font = cellFont;
  53. cell.detailTextLabel.textColor = [UIColor grayColor];
  54. }
  55. id instance = [self.instances objectAtIndex:indexPath.row];
  56. cell.textLabel.text = [NSString stringWithFormat:@"%p", instance];
  57. cell.detailTextLabel.text = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:instance];
  58. return cell;
  59. }
  60. #pragma mark - Table View Delegate
  61. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  62. {
  63. id instance = [self.instances objectAtIndex:indexPath.row];
  64. FLEXObjectExplorerViewController *drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:instance];
  65. [self.navigationController pushViewController:drillInViewController animated:YES];
  66. }
  67. @end