Created
March 14, 2013 14:32
-
-
Save andreashanft/5161808 to your computer and use it in GitHub Desktop.
Prints the hierarchy of elements in a data structure (NSDictionary, NSArray and UIView).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int depth = 0; | |
+ (void) traverseAndPrintDataStructure:(id)object | |
{ | |
NSMutableString* indent = [NSMutableString string]; | |
for (int i = 0; i < depth; i++) | |
{ | |
[indent appendString:@"\t"]; | |
} | |
if ([object isKindOfClass:[NSArray class]]) | |
{ | |
NSLog(@"%@<Array>", indent); | |
for (id value in object) | |
{ | |
depth++; | |
[MDHelper traverseAndPrintDataStructure:value]; | |
depth--; | |
} | |
NSLog(@"%@</Array>", indent); | |
} | |
else if ([object isKindOfClass:[NSDictionary class]]) | |
{ | |
NSLog(@"%@<Dict>", indent); | |
for (id key in [object allKeys]) | |
{ | |
NSLog(@"%@\"%@\":", indent, key); | |
id value = [object objectForKey:key]; | |
depth++; | |
[MDHelper traverseAndPrintDataStructure:value]; | |
depth--; | |
} | |
NSLog(@"%@</Dict>", indent); | |
} | |
else if ([object isKindOfClass:[UIView class]] && ((UIView*)object).subviews.count > 0) | |
{ | |
NSLog(@"%@<View: %@>", indent, NSStringFromClass([object class])); | |
for (id value in ((UIView*)object).subviews) | |
{ | |
depth++; | |
[MDHelper traverseAndPrintDataStructure:value]; | |
depth--; | |
} | |
NSLog(@"%@</View>", indent); | |
} | |
else | |
{ | |
NSLog(@"%@| (%@) %@", indent, NSStringFromClass([object class]), object); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment