pmk
pmk

Reputation: 1897

Objective c Custom Object Description Issue

I have some trouble logging custom objects.

An Example with a DateObject Class:

Header

@interface DateObject : NSObject {

    NSString *month;
    NSString *day;
    NSString *year;
}
@property (nonatomic, retain)NSString *month;
@property (nonatomic, retain)NSString *day;
@property (nonatomic, retain)NSString *year;
- (NSString *)description;

Implementation:

#import "DateObject.h"

@implementation DateObject
@synthesize month, day, year;

- (NSString *)description{
    NSString *result = [NSString stringWithFormat:@"{\n\tMonth:\t%@\n\tDay:\t%@\n\tYear:\t%@\n}",self.month, self.day, self.year];
    return result;
}

@end

Now I set the Values like this:

DateObject *date = [[[DateObject alloc] init] autorelease];
date.month       = @"Mar";
date.day         = @"04";
date.year        = @"2013";

Logging the Object with this

NSLog(@"{\n\tMonth:\t%@\n\tDay:\t%@\n\tYear:\t%@\n}",date.month, date.day, date.year);

results (as expected) in

2013-03-04 10:42:08.821 LoggingCustomObjectsExample[4389:c07] {
    Month:  Mar
    Day:    04
    Year:   2013
}

Now trying to log the object with

NSLog(@"%@", date);

I expect my description method to get called (it actually does get called), but the result always is unformatted:

2013-03-04 10:59:18.835 LoggingCustomObjectsExample[4389:c07] DateObject: "{\n\tMonth:\tMar\n\tDay:\t04\n\tYear:2013}";

I dont understand why the escape sequences are not working here. Am I missing something?

Upvotes: 0

Views: 612

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

This is known behaviour of NSLog and it's escaping those newline and tab characters so the output remains on a single line (see this SO question).

I would say that you don't want newline characters in your description output anyway, as they add no value.

Upvotes: 2

Related Questions