FLEXInstancesTableViewController.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. - (BOOL)prefersStatusBarHidden
  35. {
  36. return YES;
  37. }
  38. #pragma mark - Table View Data Source
  39. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  40. {
  41. return 1;
  42. }
  43. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  44. {
  45. return [self.instances count];
  46. }
  47. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  48. {
  49. static NSString *CellIdentifier = @"Cell";
  50. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  51. if (!cell) {
  52. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  53. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  54. UIFont *cellFont = [FLEXUtility defaultTableViewCellLabelFont];
  55. cell.textLabel.font = cellFont;
  56. cell.detailTextLabel.font = cellFont;
  57. cell.detailTextLabel.textColor = [UIColor grayColor];
  58. }
  59. id instance = [self.instances objectAtIndex:indexPath.row];
  60. cell.textLabel.text = [NSString stringWithFormat:@"%p", instance];
  61. cell.detailTextLabel.text = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:instance];
  62. return cell;
  63. }
  64. #pragma mark - Table View Delegate
  65. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  66. {
  67. id instance = [self.instances objectAtIndex:indexPath.row];
  68. FLEXObjectExplorerViewController *drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:instance];
  69. [self.navigationController pushViewController:drillInViewController animated:YES];
  70. }
  71. @end