FLEXObjectExplorerViewController.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. //
  2. // FLEXObjectExplorerViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-03.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXObjectExplorerViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXDescriptionTableViewCell.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. #import "FLEXPropertyEditorViewController.h"
  14. #import "FLEXIvarEditorViewController.h"
  15. #import "FLEXMethodCallingViewController.h"
  16. #import <objc/runtime.h>
  17. // Convenience boxes to keep runtime properties, ivars, and methods in foundation collections.
  18. @interface FLEXPropertyBox : NSObject
  19. @property (nonatomic, assign) objc_property_t property;
  20. @end
  21. @implementation FLEXPropertyBox
  22. @end
  23. @interface FLEXIvarBox : NSObject
  24. @property (nonatomic, assign) Ivar ivar;
  25. @end
  26. @implementation FLEXIvarBox
  27. @end
  28. @interface FLEXMethodBox : NSObject
  29. @property (nonatomic, assign) Method method;
  30. @end
  31. @implementation FLEXMethodBox
  32. @end
  33. static const NSInteger kFLEXObjectExplorerScopeNoInheritanceIndex = 0;
  34. static const NSInteger kFLEXObjectExplorerScopeIncludeInheritanceIndex = 1;
  35. @interface FLEXObjectExplorerViewController () <UISearchBarDelegate>
  36. @property (nonatomic, strong) NSArray *properties;
  37. @property (nonatomic, strong) NSArray *inheritedProperties;
  38. @property (nonatomic, strong) NSArray *filteredProperties;
  39. @property (nonatomic, strong) NSArray *ivars;
  40. @property (nonatomic, strong) NSArray *inheritedIvars;
  41. @property (nonatomic, strong) NSArray *filteredIvars;
  42. @property (nonatomic, strong) NSArray *methods;
  43. @property (nonatomic, strong) NSArray *inheritedMethods;
  44. @property (nonatomic, strong) NSArray *filteredMethods;
  45. @property (nonatomic, strong) NSArray *classMethods;
  46. @property (nonatomic, strong) NSArray *inheritedClassMethods;
  47. @property (nonatomic, strong) NSArray *filteredClassMethods;
  48. @property (nonatomic, strong) NSArray *superclasses;
  49. @property (nonatomic, strong) NSArray *filteredSuperclasses;
  50. @property (nonatomic, strong) NSArray *cachedCustomSectionRowCookies;
  51. @property (nonatomic, strong) NSIndexSet *customSectionVisibleIndexes;
  52. @property (nonatomic, strong) UISearchBar *searchBar;
  53. @property (nonatomic, strong) NSString *filterText;
  54. @property (nonatomic, assign) BOOL includeInheritance;
  55. @end
  56. @implementation FLEXObjectExplorerViewController
  57. - (id)initWithStyle:(UITableViewStyle)style
  58. {
  59. // Force grouped style
  60. return [super initWithStyle:UITableViewStyleGrouped];
  61. }
  62. - (void)viewDidLoad
  63. {
  64. [super viewDidLoad];
  65. self.searchBar = [[UISearchBar alloc] init];
  66. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  67. self.searchBar.delegate = self;
  68. self.searchBar.showsScopeBar = YES;
  69. self.searchBar.scopeButtonTitles = @[@"No Inheritance", @"Include Inheritance"];
  70. [self.searchBar sizeToFit];
  71. self.tableView.tableHeaderView = self.searchBar;
  72. self.refreshControl = [[UIRefreshControl alloc] init];
  73. [self.refreshControl addTarget:self action:@selector(refreshControlDidRefresh:) forControlEvents:UIControlEventValueChanged];
  74. }
  75. - (void)viewWillAppear:(BOOL)animated
  76. {
  77. NSMutableArray *visibleIndexPaths = [NSMutableArray array];
  78. for (UITableViewCell *cell in [self.tableView visibleCells]) {
  79. [visibleIndexPaths addObject:[self.tableView indexPathForCell:cell]];
  80. }
  81. [self.tableView reloadRowsAtIndexPaths:visibleIndexPaths withRowAnimation:UITableViewRowAnimationNone];
  82. }
  83. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  84. {
  85. [self.searchBar endEditing:YES];
  86. }
  87. - (void)refreshControlDidRefresh:(id)sender
  88. {
  89. [self updateTableData];
  90. [self.refreshControl endRefreshing];
  91. }
  92. #pragma mark - Search
  93. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  94. {
  95. self.filterText = searchText;
  96. }
  97. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  98. {
  99. if (selectedScope == kFLEXObjectExplorerScopeIncludeInheritanceIndex) {
  100. self.includeInheritance = YES;
  101. } else if (selectedScope == kFLEXObjectExplorerScopeNoInheritanceIndex) {
  102. self.includeInheritance = NO;
  103. }
  104. }
  105. #pragma mark - Setter overrides
  106. - (void)setObject:(id)object
  107. {
  108. _object = object;
  109. self.title = [[object class] description];
  110. [self updateTableData];
  111. }
  112. - (void)setIncludeInheritance:(BOOL)includeInheritance
  113. {
  114. if (_includeInheritance != includeInheritance) {
  115. _includeInheritance = includeInheritance;
  116. [self updateDisplayedData];
  117. }
  118. }
  119. - (void)setFilterText:(NSString *)filterText
  120. {
  121. if (_filterText != filterText || ![_filterText isEqual:filterText]) {
  122. _filterText = filterText;
  123. [self updateDisplayedData];
  124. }
  125. }
  126. #pragma mark - Reloading
  127. - (void)updateTableData
  128. {
  129. [self updateCustomData];
  130. [self updateProperties];
  131. [self updateIvars];
  132. [self updateMethods];
  133. [self updateClassMethods];
  134. [self updateSuperclasses];
  135. [self updateDisplayedData];
  136. }
  137. - (void)updateDisplayedData
  138. {
  139. [self updateFilteredCustomData];
  140. [self updateFilteredProperties];
  141. [self updateFilteredIvars];
  142. [self updateFilteredMethods];
  143. [self updateFilteredClassMethods];
  144. [self updateFilteredSuperclasses];
  145. [self.tableView reloadData];
  146. }
  147. - (BOOL)shouldShowDescription
  148. {
  149. BOOL showDescription = YES;
  150. // Not if it's empty or nil.
  151. NSString *descripition = [FLEXUtility safeDescriptionForObject:self.object];
  152. if (showDescription) {
  153. showDescription = [descripition length] > 0;
  154. }
  155. // Not if we have filter text that doesn't match the desctiption.
  156. if (showDescription && [self.filterText length] > 0) {
  157. showDescription = [descripition rangeOfString:self.filterText options:NSCaseInsensitiveSearch].length > 0;
  158. }
  159. return showDescription;
  160. }
  161. #pragma mark - Properties
  162. - (void)updateProperties
  163. {
  164. Class class = [self.object class];
  165. self.properties = [[self class] propertiesForClass:class];
  166. self.inheritedProperties = [[self class] inheritedPropertiesForClass:class];
  167. }
  168. + (NSArray *)propertiesForClass:(Class)class
  169. {
  170. NSMutableArray *boxedProperties = [NSMutableArray array];
  171. unsigned int propertyCount = 0;
  172. objc_property_t *propertyList = class_copyPropertyList(class, &propertyCount);
  173. if (propertyList) {
  174. for (int i = 0; i < propertyCount; i++) {
  175. FLEXPropertyBox *propertyBox = [[FLEXPropertyBox alloc] init];
  176. propertyBox.property = propertyList[i];
  177. [boxedProperties addObject:propertyBox];
  178. }
  179. free(propertyList);
  180. }
  181. return boxedProperties;
  182. }
  183. + (NSArray *)inheritedPropertiesForClass:(Class)class
  184. {
  185. NSMutableArray *inheritedProperties = [NSMutableArray array];
  186. while ((class = [class superclass])) {
  187. [inheritedProperties addObjectsFromArray:[self propertiesForClass:class]];
  188. }
  189. return inheritedProperties;
  190. }
  191. - (void)updateFilteredProperties
  192. {
  193. NSArray *candidateProperties = self.properties;
  194. if (self.includeInheritance) {
  195. candidateProperties = [candidateProperties arrayByAddingObjectsFromArray:self.inheritedProperties];
  196. }
  197. NSArray *unsortedFilteredProperties = nil;
  198. if ([self.filterText length] > 0) {
  199. NSMutableArray *mutableUnsortedFilteredProperties = [NSMutableArray array];
  200. for (FLEXPropertyBox *propertyBox in candidateProperties) {
  201. NSString *prettyName = [FLEXRuntimeUtility prettyNameForProperty:propertyBox.property];
  202. if ([prettyName rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  203. [mutableUnsortedFilteredProperties addObject:propertyBox];
  204. }
  205. }
  206. unsortedFilteredProperties = mutableUnsortedFilteredProperties;
  207. } else {
  208. unsortedFilteredProperties = candidateProperties;
  209. }
  210. self.filteredProperties = [unsortedFilteredProperties sortedArrayUsingComparator:^NSComparisonResult(FLEXPropertyBox *propertyBox1, FLEXPropertyBox *propertyBox2) {
  211. NSString *name1 = [NSString stringWithUTF8String:property_getName(propertyBox1.property)];
  212. NSString *name2 = [NSString stringWithUTF8String:property_getName(propertyBox2.property)];
  213. return [name1 caseInsensitiveCompare:name2];
  214. }];
  215. }
  216. - (NSString *)titleForPropertyAtIndex:(NSInteger)index
  217. {
  218. FLEXPropertyBox *propertyBox = [self.filteredProperties objectAtIndex:index];
  219. return [FLEXRuntimeUtility prettyNameForProperty:propertyBox.property];
  220. }
  221. - (id)valueForPropertyAtIndex:(NSInteger)index
  222. {
  223. id value = nil;
  224. if ([self canHaveInstanceState]) {
  225. FLEXPropertyBox *propertyBox = [self.filteredProperties objectAtIndex:index];
  226. value = [FLEXRuntimeUtility valueForProperty:propertyBox.property onObject:self.object];
  227. }
  228. return value;
  229. }
  230. #pragma mark - Ivars
  231. - (void)updateIvars
  232. {
  233. Class class = [self.object class];
  234. self.ivars = [[self class] ivarsForClass:class];
  235. self.inheritedIvars = [[self class] inheritedIvarsForClass:class];
  236. }
  237. + (NSArray *)ivarsForClass:(Class)class
  238. {
  239. NSMutableArray *boxedIvars = [NSMutableArray array];
  240. unsigned int ivarCount = 0;
  241. Ivar *ivarList = class_copyIvarList(class, &ivarCount);
  242. if (ivarList) {
  243. for (int i = 0; i < ivarCount; i++) {
  244. FLEXIvarBox *ivarBox = [[FLEXIvarBox alloc] init];
  245. ivarBox.ivar = ivarList[i];
  246. [boxedIvars addObject:ivarBox];
  247. }
  248. free(ivarList);
  249. }
  250. return boxedIvars;
  251. }
  252. + (NSArray *)inheritedIvarsForClass:(Class)class
  253. {
  254. NSMutableArray *inheritedIvars = [NSMutableArray array];
  255. while ((class = [class superclass])) {
  256. [inheritedIvars addObjectsFromArray:[self ivarsForClass:class]];
  257. }
  258. return inheritedIvars;
  259. }
  260. - (void)updateFilteredIvars
  261. {
  262. NSArray *candidateIvars = self.ivars;
  263. if (self.includeInheritance) {
  264. candidateIvars = [candidateIvars arrayByAddingObjectsFromArray:self.inheritedIvars];
  265. }
  266. NSArray *unsortedFilteredIvars = nil;
  267. if ([self.filterText length] > 0) {
  268. NSMutableArray *mutableUnsortedFilteredIvars = [NSMutableArray array];
  269. for (FLEXIvarBox *ivarBox in candidateIvars) {
  270. NSString *prettyName = [FLEXRuntimeUtility prettyNameForIvar:ivarBox.ivar];
  271. if ([prettyName rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  272. [mutableUnsortedFilteredIvars addObject:ivarBox];
  273. }
  274. }
  275. unsortedFilteredIvars = mutableUnsortedFilteredIvars;
  276. } else {
  277. unsortedFilteredIvars = candidateIvars;
  278. }
  279. self.filteredIvars = [unsortedFilteredIvars sortedArrayUsingComparator:^NSComparisonResult(FLEXIvarBox *ivarBox1, FLEXIvarBox *ivarBox2) {
  280. NSString *name1 = [NSString stringWithUTF8String:ivar_getName(ivarBox1.ivar)];
  281. NSString *name2 = [NSString stringWithUTF8String:ivar_getName(ivarBox2.ivar)];
  282. return [name1 caseInsensitiveCompare:name2];
  283. }];
  284. }
  285. - (NSString *)titleForIvarAtIndex:(NSInteger)index
  286. {
  287. FLEXIvarBox *ivarBox = [self.filteredIvars objectAtIndex:index];
  288. return [FLEXRuntimeUtility prettyNameForIvar:ivarBox.ivar];
  289. }
  290. - (id)valueForIvarAtIndex:(NSInteger)index
  291. {
  292. id value = nil;
  293. if (![self canHaveInstanceState]) {
  294. FLEXIvarBox *ivarBox = [self.filteredIvars objectAtIndex:index];
  295. value = [FLEXRuntimeUtility valueForIvar:ivarBox.ivar onObject:self.object];
  296. }
  297. return value;
  298. }
  299. #pragma mark - Methods
  300. - (void)updateMethods
  301. {
  302. Class class = [self.object class];
  303. self.methods = [[self class] methodsForClass:class];
  304. self.inheritedMethods = [[self class] inheritedMethodsForClass:class];
  305. }
  306. - (void)updateFilteredMethods
  307. {
  308. self.filteredMethods = [self filteredMethodsFromMethods:self.methods inheritedMethods:self.inheritedMethods areClassMethods:NO];
  309. }
  310. - (void)updateClassMethods
  311. {
  312. const char *className = [NSStringFromClass([self.object class]) UTF8String];
  313. Class metaClass = objc_getMetaClass(className);
  314. self.classMethods = [[self class] methodsForClass:metaClass];
  315. self.inheritedClassMethods = [[self class] inheritedMethodsForClass:metaClass];
  316. }
  317. - (void)updateFilteredClassMethods
  318. {
  319. self.filteredClassMethods = [self filteredMethodsFromMethods:self.classMethods inheritedMethods:self.inheritedClassMethods areClassMethods:YES];
  320. }
  321. + (NSArray *)methodsForClass:(Class)class
  322. {
  323. NSMutableArray *boxedMethods = [NSMutableArray array];
  324. unsigned int methodCount = 0;
  325. Method *methodList = class_copyMethodList(class, &methodCount);
  326. if (methodList) {
  327. for (int i = 0; i < methodCount; i++) {
  328. FLEXMethodBox *methodBox = [[FLEXMethodBox alloc] init];
  329. methodBox.method = methodList[i];
  330. [boxedMethods addObject:methodBox];
  331. }
  332. free(methodList);
  333. }
  334. return boxedMethods;
  335. }
  336. + (NSArray *)inheritedMethodsForClass:(Class)class
  337. {
  338. NSMutableArray *inheritedMethods = [NSMutableArray array];
  339. while ((class = [class superclass])) {
  340. [inheritedMethods addObjectsFromArray:[self methodsForClass:class]];
  341. }
  342. return inheritedMethods;
  343. }
  344. - (NSArray *)filteredMethodsFromMethods:(NSArray *)methods inheritedMethods:(NSArray *)inheritedMethods areClassMethods:(BOOL)areClassMethods
  345. {
  346. NSArray *candidateMethods = methods;
  347. if (self.includeInheritance) {
  348. candidateMethods = [candidateMethods arrayByAddingObjectsFromArray:inheritedMethods];
  349. }
  350. NSArray *unsortedFilteredMethods = nil;
  351. if ([self.filterText length] > 0) {
  352. NSMutableArray *mutableUnsortedFilteredMethods = [NSMutableArray array];
  353. for (FLEXMethodBox *methodBox in candidateMethods) {
  354. NSString *prettyName = [FLEXRuntimeUtility prettyNameForMethod:methodBox.method isClassMethod:areClassMethods];
  355. if ([prettyName rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  356. [mutableUnsortedFilteredMethods addObject:methodBox];
  357. }
  358. }
  359. unsortedFilteredMethods = mutableUnsortedFilteredMethods;
  360. } else {
  361. unsortedFilteredMethods = candidateMethods;
  362. }
  363. NSArray *sortedFilteredMethods = [unsortedFilteredMethods sortedArrayUsingComparator:^NSComparisonResult(FLEXMethodBox *methodBox1, FLEXMethodBox *methodBox2) {
  364. NSString *name1 = NSStringFromSelector(method_getName(methodBox1.method));
  365. NSString *name2 = NSStringFromSelector(method_getName(methodBox2.method));
  366. return [name1 caseInsensitiveCompare:name2];
  367. }];
  368. return sortedFilteredMethods;
  369. }
  370. - (NSString *)titleForMethodAtIndex:(NSInteger)index
  371. {
  372. FLEXMethodBox *methodBox = [self.filteredMethods objectAtIndex:index];
  373. return [FLEXRuntimeUtility prettyNameForMethod:methodBox.method isClassMethod:NO];
  374. }
  375. - (NSString *)titleForClassMethodAtIndex:(NSInteger)index
  376. {
  377. FLEXMethodBox *classMethodBox = [self.filteredClassMethods objectAtIndex:index];
  378. return [FLEXRuntimeUtility prettyNameForMethod:classMethodBox.method isClassMethod:YES];
  379. }
  380. #pragma mark - Superclasses
  381. + (NSArray *)superclassesForClass:(Class)class
  382. {
  383. NSMutableArray *superClasses = [NSMutableArray array];
  384. while ((class = [class superclass])) {
  385. [superClasses addObject:class];
  386. }
  387. return superClasses;
  388. }
  389. - (void)updateSuperclasses
  390. {
  391. self.superclasses = [[self class] superclassesForClass:[self.object class]];
  392. }
  393. - (void)updateFilteredSuperclasses
  394. {
  395. if ([self.filterText length] > 0) {
  396. NSMutableArray *filteredSuperclasses = [NSMutableArray array];
  397. for (Class superclass in self.superclasses) {
  398. if ([NSStringFromClass(superclass) rangeOfString:self.filterText options:NSCaseInsensitiveSearch].length > 0) {
  399. [filteredSuperclasses addObject:superclass];
  400. }
  401. }
  402. self.filteredSuperclasses = filteredSuperclasses;
  403. } else {
  404. self.filteredSuperclasses = self.superclasses;
  405. }
  406. }
  407. #pragma mark - Table View Data Helpers
  408. - (NSArray *)possibleExplorerSections
  409. {
  410. static NSArray *possibleSections = nil;
  411. static dispatch_once_t onceToken;
  412. dispatch_once(&onceToken, ^{
  413. possibleSections = @[@(FLEXObjectExplorerSectionDescription),
  414. @(FLEXObjectExplorerSectionCustom),
  415. @(FLEXObjectExplorerSectionProperties),
  416. @(FLEXObjectExplorerSectionIvars),
  417. @(FLEXObjectExplorerSectionMethods),
  418. @(FLEXObjectExplorerSectionClassMethods),
  419. @(FLEXObjectExplorerSectionSuperclasses)];
  420. });
  421. return possibleSections;
  422. }
  423. - (NSArray *)visibleExplorerSections
  424. {
  425. NSMutableArray *visibleSections = [NSMutableArray array];
  426. for (NSNumber *possibleSection in [self possibleExplorerSections]) {
  427. FLEXObjectExplorerSection explorerSection = [possibleSection unsignedIntegerValue];
  428. if ([self numberOfRowsForExplorerSection:explorerSection] > 0) {
  429. [visibleSections addObject:possibleSection];
  430. }
  431. }
  432. return visibleSections;
  433. }
  434. - (NSString *)sectionTitleWithBaseName:(NSString *)baseName totalCount:(NSUInteger)totalCount filteredCount:(NSUInteger)filteredCount
  435. {
  436. NSString *sectionTitle = nil;
  437. if (totalCount == filteredCount) {
  438. sectionTitle = [baseName stringByAppendingFormat:@" (%lu)", (unsigned long)totalCount];
  439. } else {
  440. sectionTitle = [baseName stringByAppendingFormat:@" (%lu of %lu)", (unsigned long)filteredCount, (unsigned long)totalCount];
  441. }
  442. return sectionTitle;
  443. }
  444. - (FLEXObjectExplorerSection)explorerSectionAtIndex:(NSInteger)sectionIndex
  445. {
  446. return [[[self visibleExplorerSections] objectAtIndex:sectionIndex] unsignedIntegerValue];
  447. }
  448. - (NSInteger)numberOfRowsForExplorerSection:(FLEXObjectExplorerSection)section
  449. {
  450. NSInteger numberOfRows = 0;
  451. switch (section) {
  452. case FLEXObjectExplorerSectionDescription:
  453. numberOfRows = [self shouldShowDescription] ? 1 : 0;
  454. break;
  455. case FLEXObjectExplorerSectionCustom:
  456. numberOfRows = [self.customSectionVisibleIndexes count];
  457. break;
  458. case FLEXObjectExplorerSectionProperties:
  459. numberOfRows = [self.filteredProperties count];
  460. break;
  461. case FLEXObjectExplorerSectionIvars:
  462. numberOfRows = [self.filteredIvars count];
  463. break;
  464. case FLEXObjectExplorerSectionMethods:
  465. numberOfRows = [self.filteredMethods count];
  466. break;
  467. case FLEXObjectExplorerSectionClassMethods:
  468. numberOfRows = [self.filteredClassMethods count];
  469. break;
  470. case FLEXObjectExplorerSectionSuperclasses:
  471. numberOfRows = [self.filteredSuperclasses count];
  472. break;
  473. }
  474. return numberOfRows;
  475. }
  476. - (NSString *)titleForRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  477. {
  478. NSString *title = nil;
  479. switch (section) {
  480. case FLEXObjectExplorerSectionDescription:
  481. title = [FLEXUtility safeDescriptionForObject:self.object];
  482. break;
  483. case FLEXObjectExplorerSectionCustom:
  484. title = [self customSectionTitleForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  485. break;
  486. case FLEXObjectExplorerSectionProperties:
  487. title = [self titleForPropertyAtIndex:row];
  488. break;
  489. case FLEXObjectExplorerSectionIvars:
  490. title = [self titleForIvarAtIndex:row];
  491. break;
  492. case FLEXObjectExplorerSectionMethods:
  493. title = [self titleForMethodAtIndex:row];
  494. break;
  495. case FLEXObjectExplorerSectionClassMethods:
  496. title = [self titleForClassMethodAtIndex:row];
  497. break;
  498. case FLEXObjectExplorerSectionSuperclasses:
  499. title = NSStringFromClass([self.filteredSuperclasses objectAtIndex:row]);
  500. }
  501. return title;
  502. }
  503. - (NSString *)subtitleForRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  504. {
  505. NSString *subtitle = nil;
  506. switch (section) {
  507. case FLEXObjectExplorerSectionDescription:
  508. break;
  509. case FLEXObjectExplorerSectionCustom:
  510. subtitle = [self customSectionSubtitleForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  511. break;
  512. case FLEXObjectExplorerSectionProperties:
  513. subtitle = [self canHaveInstanceState] ? [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[self valueForPropertyAtIndex:row]] : nil;
  514. break;
  515. case FLEXObjectExplorerSectionIvars:
  516. subtitle = [self canHaveInstanceState] ? [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[self valueForIvarAtIndex:row]] : nil;
  517. break;
  518. case FLEXObjectExplorerSectionMethods:
  519. break;
  520. case FLEXObjectExplorerSectionClassMethods:
  521. break;
  522. case FLEXObjectExplorerSectionSuperclasses:
  523. break;
  524. }
  525. return subtitle;
  526. }
  527. - (BOOL)canDrillInToRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  528. {
  529. BOOL canDrillIn = NO;
  530. switch (section) {
  531. case FLEXObjectExplorerSectionDescription:
  532. break;
  533. case FLEXObjectExplorerSectionCustom:
  534. canDrillIn = [self customSectionCanDrillIntoRowWithCookie:[self customSectionRowCookieForVisibleRow:row]];
  535. break;
  536. case FLEXObjectExplorerSectionProperties: {
  537. if ([self canHaveInstanceState]) {
  538. objc_property_t property = [[self.filteredProperties objectAtIndex:row] property];
  539. id currentValue = [self valueForPropertyAtIndex:row];
  540. BOOL canEdit = [FLEXPropertyEditorViewController canEditProperty:property currentValue:currentValue];
  541. BOOL canExplore = currentValue != nil;
  542. canDrillIn = canEdit || canExplore;
  543. }
  544. } break;
  545. case FLEXObjectExplorerSectionIvars: {
  546. if ([self canHaveInstanceState]) {
  547. Ivar ivar = [[self.filteredIvars objectAtIndex:row] ivar];
  548. id currentValue = [self valueForIvarAtIndex:row];
  549. BOOL canEdit = [FLEXIvarEditorViewController canEditIvar:ivar currentValue:currentValue];
  550. BOOL canExplore = currentValue != nil;
  551. canDrillIn = canEdit || canExplore;
  552. }
  553. } break;
  554. case FLEXObjectExplorerSectionMethods:
  555. canDrillIn = [self canCallInstanceMethods];
  556. break;
  557. case FLEXObjectExplorerSectionClassMethods:
  558. canDrillIn = YES;
  559. break;
  560. case FLEXObjectExplorerSectionSuperclasses:
  561. canDrillIn = YES;
  562. break;
  563. }
  564. return canDrillIn;
  565. }
  566. - (NSString *)titleForExplorerSection:(FLEXObjectExplorerSection)section
  567. {
  568. NSString *title = nil;
  569. switch (section) {
  570. case FLEXObjectExplorerSectionDescription: {
  571. title = @"Description";
  572. } break;
  573. case FLEXObjectExplorerSectionCustom: {
  574. title = [self customSectionTitle];
  575. } break;
  576. case FLEXObjectExplorerSectionProperties: {
  577. NSUInteger totalCount = [self.properties count];
  578. if (self.includeInheritance) {
  579. totalCount += [self.inheritedProperties count];
  580. }
  581. title = [self sectionTitleWithBaseName:@"Properties" totalCount:totalCount filteredCount:[self.filteredProperties count]];
  582. } break;
  583. case FLEXObjectExplorerSectionIvars: {
  584. NSUInteger totalCount = [self.ivars count];
  585. if (self.includeInheritance) {
  586. totalCount += [self.inheritedIvars count];
  587. }
  588. title = [self sectionTitleWithBaseName:@"Ivars" totalCount:totalCount filteredCount:[self.filteredIvars count]];
  589. } break;
  590. case FLEXObjectExplorerSectionMethods: {
  591. NSUInteger totalCount = [self.methods count];
  592. if (self.includeInheritance) {
  593. totalCount += [self.inheritedMethods count];
  594. }
  595. title = [self sectionTitleWithBaseName:@"Methods" totalCount:totalCount filteredCount:[self.filteredMethods count]];
  596. } break;
  597. case FLEXObjectExplorerSectionClassMethods: {
  598. NSUInteger totalCount = [self.classMethods count];
  599. if (self.includeInheritance) {
  600. totalCount += [self.inheritedClassMethods count];
  601. }
  602. title = [self sectionTitleWithBaseName:@"Class Methods" totalCount:totalCount filteredCount:[self.filteredClassMethods count]];
  603. } break;
  604. case FLEXObjectExplorerSectionSuperclasses: {
  605. title = [self sectionTitleWithBaseName:@"Superclasses" totalCount:[self.superclasses count] filteredCount:[self.filteredSuperclasses count]];
  606. } break;
  607. }
  608. return title;
  609. }
  610. - (UIViewController *)drillInViewControllerForRow:(NSUInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  611. {
  612. UIViewController *viewController = nil;
  613. switch (section) {
  614. case FLEXObjectExplorerSectionDescription:
  615. break;
  616. case FLEXObjectExplorerSectionCustom:
  617. viewController = [self customSectionDrillInViewControllerForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  618. break;
  619. case FLEXObjectExplorerSectionProperties: {
  620. objc_property_t property = [[self.filteredProperties objectAtIndex:row] property];
  621. id currentValue = [self valueForPropertyAtIndex:row];
  622. if ([FLEXPropertyEditorViewController canEditProperty:property currentValue:currentValue]) {
  623. viewController = [[FLEXPropertyEditorViewController alloc] initWithTarget:self.object property:property];
  624. } else if (currentValue) {
  625. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:currentValue];
  626. }
  627. } break;
  628. case FLEXObjectExplorerSectionIvars: {
  629. Ivar ivar = [[self.filteredIvars objectAtIndex:row] ivar];
  630. id currentValue = [self valueForIvarAtIndex:row];
  631. if ([FLEXIvarEditorViewController canEditIvar:ivar currentValue:currentValue]) {
  632. viewController = [[FLEXIvarEditorViewController alloc] initWithTarget:self.object ivar:ivar];
  633. } else if (currentValue) {
  634. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:currentValue];
  635. }
  636. } break;
  637. case FLEXObjectExplorerSectionMethods: {
  638. Method method = [[self.filteredMethods objectAtIndex:row] method];
  639. viewController = [[FLEXMethodCallingViewController alloc] initWithTarget:self.object method:method];
  640. } break;
  641. case FLEXObjectExplorerSectionClassMethods: {
  642. Method method = [[self.filteredClassMethods objectAtIndex:row] method];
  643. viewController = [[FLEXMethodCallingViewController alloc] initWithTarget:[self.object class] method:method];
  644. } break;
  645. case FLEXObjectExplorerSectionSuperclasses: {
  646. Class superclass = [self.filteredSuperclasses objectAtIndex:row];
  647. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:superclass];
  648. }
  649. }
  650. return viewController;
  651. }
  652. #pragma mark - Table View Data Source
  653. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  654. {
  655. return [[self visibleExplorerSections] count];
  656. }
  657. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  658. {
  659. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:section];
  660. return [self numberOfRowsForExplorerSection:explorerSection];
  661. }
  662. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  663. {
  664. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:section];
  665. return [self titleForExplorerSection:explorerSection];
  666. }
  667. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  668. {
  669. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  670. BOOL useDescriptionCell = explorerSection == FLEXObjectExplorerSectionDescription;
  671. NSString *cellIdentifier = useDescriptionCell ? @"descriptionCell" : @"cell";
  672. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  673. if (!cell) {
  674. if (useDescriptionCell) {
  675. cell = [[FLEXDescriptionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  676. } else {
  677. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  678. UIFont *cellFont = [FLEXUtility defaultTableViewCellLabelFont];
  679. cell.textLabel.font = cellFont;
  680. cell.detailTextLabel.font = cellFont;
  681. cell.detailTextLabel.textColor = [UIColor grayColor];
  682. }
  683. }
  684. cell.textLabel.text = [self titleForRow:indexPath.row inExplorerSection:explorerSection];
  685. cell.detailTextLabel.text = [self subtitleForRow:indexPath.row inExplorerSection:explorerSection];
  686. cell.accessoryType = [self canDrillInToRow:indexPath.row inExplorerSection:explorerSection] ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
  687. return cell;
  688. }
  689. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  690. {
  691. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  692. CGFloat height = self.tableView.rowHeight;
  693. if (explorerSection == FLEXObjectExplorerSectionDescription) {
  694. NSString *text = [self titleForRow:indexPath.row inExplorerSection:explorerSection];
  695. CGFloat preferredHeight = [FLEXDescriptionTableViewCell preferredHeightWithText:text inTableViewWidth:self.tableView.frame.size.width];
  696. height = MAX(height, preferredHeight);
  697. }
  698. return height;
  699. }
  700. #pragma mark - Table View Delegate
  701. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  702. {
  703. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  704. return [self canDrillInToRow:indexPath.row inExplorerSection:explorerSection];
  705. }
  706. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  707. {
  708. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  709. UIViewController *detailViewController = [self drillInViewControllerForRow:indexPath.row inExplorerSection:explorerSection];
  710. if (detailViewController) {
  711. [self.navigationController pushViewController:detailViewController animated:YES];
  712. } else {
  713. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  714. }
  715. }
  716. #pragma mark - Custom Section
  717. - (void)updateCustomData
  718. {
  719. self.cachedCustomSectionRowCookies = [self customSectionRowCookies];
  720. }
  721. - (void)updateFilteredCustomData
  722. {
  723. NSIndexSet *filteredIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self.cachedCustomSectionRowCookies count])];
  724. if ([self.filterText length] > 0) {
  725. filteredIndexSet = [filteredIndexSet indexesPassingTest:^BOOL(NSUInteger index, BOOL *stop) {
  726. BOOL matches = NO;
  727. NSString *rowTitle = [self customSectionTitleForRowCookie:[self.cachedCustomSectionRowCookies objectAtIndex:index]];
  728. if ([rowTitle rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  729. matches = YES;
  730. }
  731. return matches;
  732. }];
  733. }
  734. self.customSectionVisibleIndexes = filteredIndexSet;
  735. }
  736. - (id)customSectionRowCookieForVisibleRow:(NSUInteger)row
  737. {
  738. return [[self.cachedCustomSectionRowCookies objectsAtIndexes:self.customSectionVisibleIndexes] objectAtIndex:row];
  739. }
  740. #pragma mark - Subclasses Can Override
  741. - (NSString *)customSectionTitle
  742. {
  743. return nil;
  744. }
  745. - (NSArray *)customSectionRowCookies
  746. {
  747. return nil;
  748. }
  749. - (NSString *)customSectionTitleForRowCookie:(id)rowCookie
  750. {
  751. return nil;
  752. }
  753. - (NSString *)customSectionSubtitleForRowCookie:(id)rowCookie
  754. {
  755. return nil;
  756. }
  757. - (BOOL)customSectionCanDrillIntoRowWithCookie:(id)rowCookie
  758. {
  759. return NO;
  760. }
  761. - (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
  762. {
  763. return nil;
  764. }
  765. - (BOOL)canHaveInstanceState
  766. {
  767. return YES;
  768. }
  769. - (BOOL)canCallInstanceMethods
  770. {
  771. return YES;
  772. }
  773. @end