Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override description or stringValue in cocoa?

I want to have an descriptive string for an object in Cocoa. I'm thinking about overriding either the description method or the stringValue method. Which is preferable and why? The only guideline I could find was in here stating

You are discouraged from overriding description.

Is this indeed what you would suggest? Any other preferred overrride point?

like image 424
mvexel Avatar asked Dec 14 '09 10:12

mvexel


2 Answers

I personally override description in virtually all subclasses I create. I guess, like Tom Duckering writes in his comment, that your quote only applies to Managed Objects.

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ <%p>", NSStringFromClass([self class]), self];
}
like image 116
Pascal Avatar answered Oct 04 '22 22:10

Pascal


description is the way to go, that's what it's called to supply string representation of an object.

- (NSString*)description
{
    return [NSString stringWithFormat:@"%@, %@; %@", a, b, c];
}

I believe suggested by Hillegass' book as well.

like image 31
stefanB Avatar answered Oct 04 '22 21:10

stefanB