However, the description method can be called also directly.
// main.m
#import <Foundation/Foundation.h>
// Every class is a subclass of NSObject class, either directly and indirectly.
@interface Foo : NSObject
@end
@interface FooBar : Foo
@end
@interface InvisibleFooBar : FooBar
@end
int main (int argc, const char * argv[])
{
@autoreleasepool {
// Let NSLog call the description method
Foo *foo = [Foo new];
NSLog (@"%s: %@", __func__, foo);
FooBar *fooBar = [FooBar new];
NSLog (@"%s: %@", __func__, fooBar);
InvisibleFooBar *invFooBar = [InvisibleFooBar new];
NSLog (@"%s: %@", __func__, invFooBar);
// Call the description method directly
FooBar *fooBar2 = [FooBar new];
NSLog (@"%s: %@", __func__, [fooBar2 description]);
}
return 0;
}
@implementation Foo
// Use superclass's description
@end
@implementation FooBar
// Override
- (NSString *)description
{
return [NSString stringWithFormat:@"I am a FooBar at %p", self];
}
@end
@implementation InvisibleFooBar
// Override
- (NSString *)description
{
return @"";
}
@end
The description method is defined by the mother of all classes: NSObject. If a class does not override the description method, it will automatically inherit the one from it superclass.