3

I have an object with two properties - type and name - which I want to show in its description. The out-of-the-box description looks like this:

<SGBMessage: 0x7663bb0>

If I override description, like so:

return [NSString stringWithFormat:@"<%@: %x type:%@ name%@>", 
           [self class], (int)self, self.type, self.name];

Then I can get a nice description like this:

<SGBMessage: 0x7663bb0 type:loadScreen name:mainScreen>

So far, so good. But Apple's objects have dynamic descriptions; if I look at a view's description I get this:

<UIView: 0x767bcb0; frame = (0 0; 0 0); layer = <CALayer: 0x767bd50>>

But if I set hidden to true, I get this:

<UIView: 0x767bcb0; frame = (0 0; 0 0); hidden = YES;
 layer = <CALayer: 0x767bd50>>

Now, I don't believe for a second that they've got a massive set of if statements in the description methods of all of their objects; it seems much more likely that there's some method in some category somewhere on NSObject that can be overridden to specify which properties show up in the description. Does anyone know what's really going on, and if so, is it something I can take advantage of?

Simon
  • 25,468
  • 44
  • 152
  • 266

1 Answers1

1

Mine tend to follow this pattern:

- (NSString *) description {
    NSMutableDictionary *descriptionDict = [[NSMutableDictionary alloc]init];
    if (account)       [descriptionDict setObject:account       forKey:@"account"];
    if (date)          [descriptionDict setObject:date          forKey:@"date"];
    if (contentString) [descriptionDict setObject:contentString forKey:@"contentString"];
    return [descriptionDict description];
}

You could use a similar approach to build an NSMutableArray, and then iterate through the array, adding what's in there to the string.

For more complex apps, if you have custom classes that inherit from other classes, you can also make a separate method that returns descriptionDict, and then in the subclass call NSMutableDictionary *descriptionDict = [super descriptionDict] and continue adding / removing elements to it.

NOTE: The reason I use if statements on each line is that if one object happens to be nil, an exception is thrown. This will cause "no objective c description available" to print when you try to po your object.

But to answer your question, there's no secret way to make certain properties appear in the description. You just have to build a string yourself, by whatever means you decide is appropriate.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks. That is indeed the "massive set of if statements" approach that I allude to in the question. – Simon Dec 21 '12 at 23:29
  • @Simon The only way around it would be to make a method that takes an array of keys, checks if they're assigned, and returns their values. That is, instead of a "massive set of if statements", you have an array that you iterate through and use key value observing to only have one "if" statement. – Aaron Brager Dec 22 '12 at 15:12
  • @Simon However, that seems more complex than necessary. For example, see http://stackoverflow.com/questions/4874288/how-can-i-get-a-callback-by-kvo-on-a-uiviews-frame-property on using KVO to get the value of a UIView's frame. – Aaron Brager Dec 22 '12 at 15:13