Skip to content

Instantly share code, notes, and snippets.

@andreashanft
Created March 14, 2013 14:32
Show Gist options
  • Save andreashanft/5161808 to your computer and use it in GitHub Desktop.
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).
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