-1

I learnt from here how to iterate in objective c over a UIView / log a view hierarchy, but the question is too old to comment on it:

// UIView+HierarchyLogging.h
@interface UIView (ViewHierarchyLogging)
- (void)logViewHierarchy;
@end

// UIView+HierarchyLogging.m
@implementation UIView (ViewHierarchyLogging)
- (void)logViewHierarchy
{
   NSLog(@"%@", self);
   for (UIView *subview in self.subviews)
   {
       [subview logViewHierarchy];
   }
}
@end

// In your implementation
[myView logViewHierarchy];

My problem: in debugging mode everything works just fine if - and only if - I set a breakpoint in the for loop and click in XCode on "Quick look". After that all subviews are logged correctly. In a plain run only a few subviews are logged. What am I doing wrong?

Thanks in advance Joe

swarley
  • 355
  • 2
  • 8
  • Hmmm... I'm not seeing any issues. You may need to post some more info, or maybe put up a simple project that reproduces it? – DonMag Oct 12 '18 at 15:05
  • No, sorry, I can't. The behavior is part of a thirdparty sdk I can't share. Question is: What does XCodes "Quick look" do or change? – swarley Oct 15 '18 at 06:02

2 Answers2

0

I think the linked answer is theoretically correct, but it's a bit unpractical when you actually want to make sense of the output. In the output as it is, you don't see any hierarchy, because you don't see which view is a child of which view. You should at least make the hierarchy a bit visible. For example:

some-view
another-view
whatever
foo
hello you

doesn't look so well. It's better when it looks like this:

(V:) some-view
  (V:) another-view
    (V:) whatever
    (V:) foo
  (V:) hello you

In the second snippet you see that "another-view" and "hello you" are both subviews of "some-view". And you can also see that logViewHierarchy has been called exactly once. In the first (hypothetical) debug output there is no way to know how often logViewHierarchy has been called. And with the "(V:)" in the output, you know which debug output lines are from a call to logViewHierarchy, and which debug output lines are from somewhere else! Its almost never a good idea to just log an object with NSLog(@"%@", obj);; there should always be a log message at the beginning, so that you can search for it in the source code. (Most internet tutorials use NSLog in a way that is totally ill-suited for a production environment, IMHO.)

// UIView+HierarchyLogging.m
@implementation UIView (ViewHierarchyLogging)
- (void)_logViewHierarchyWithIndent:(NSString *)indentation
{
    NSLog(@"%@(V:) %@", indentation, self);
    NSString *more_indentation = [indentation stringByAppendingString:@"  "];
    for(UIView *v in self.subviews) {
        [v _logViewHierarchyWithIndent: more_indentation];
    }
}
- (void)logViewHierarchy
{
    [self _logViewHierarchyWithIndent:@""];
}
@end
Michael
  • 6,451
  • 5
  • 31
  • 53
0

The code to log subviews is correct and Michaels answer shows how to do it in a structured way. Problem was that the thirdparty sdk added the subviews and components after the 'viewDidAppear' phase where I was logging.

Thanks anyway and sorry for the confusion

swarley
  • 355
  • 2
  • 8