fuzzygoat
fuzzygoat

Reputation: 26223

Description to return just ClassName?

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-

Upvotes: 7

Views: 3790

Answers (1)

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

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])

Upvotes: 20

Related Questions