views:

122

answers:

1

The default description for a class instance returns "ClassName: 0x105120". How might I modify the method below to just return the "ClassName"?

// The code below just returns the address ...
- (NSString *)description {

 NSString *result;

 result = [NSString stringWithFormat:@"%p", self];
    return result;
}

EDIT: in that case would this be correct? Although I do understand that if I want to actually get the className as an NSString I should use NSStringFromClass([self class])

- (id)init {
 NSLog(@"_init: %@", [self class]);
 [super init];
 return self;
}

thanks in advance -gary-

+7  A: 

iPhoneOS: NSStringFromClass([self class])
MacOS: [self className]

... gives you an NSString with the class name

Edit:

For both iPhoneOS and MacOS the way to go is:

NSStringFromClass([self class])

Nikolai Ruhe
`NSStringFromClass` is actually the proper way to do it on Mac OS X too. `-className` is part of a category on NSObject meant for scripting.
kperryua
I was not aware of that, thanks!
Nikolai Ruhe