2013-03-04 2 views
0

사용자 정의 객체를 기록하는 데 문제가 있습니다. DateObject 클래스와목표 c 사용자 정의 객체 설명 문제

예제 :

헤더

@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; 

구현 : 개체 로깅

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

:

이제
#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 

내가이 같은 값을 설정지금은 내 description 방법은 전화를받을 것으로 예상

NSLog(@"%@", date); 

와 객체를 로그인을 시도

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

에서 (예상대로)이

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

결과 (실제로 호출되는 않는), 결과는 항상 형식이 지정되지 않습니다.

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

왜 여기에 이스케이프 시퀀스가 ​​작동하지 않는지 이해가되지 않습니다. 내가 빠진 것이 있습니까?

답변

2

이것은 알려진 동작으로 NSLog이며이 줄 바꿈 문자와 탭 문자를 이스케이프 처리하므로 출력이 한 줄로 유지됩니다 (이 SO question 참조).

아무런 가치도 없기 때문에 어쨌든 description 출력에 개행 문자를 넣지 않기를 바랍니다.

+0

빠른 답장을 보내 주셔서 감사 드리며, 알려진 동작이었습니다. 나는 방금 NSArray를 로깅하는 것과 같이 형식화 된 로그를 갖는 것이 좋을 것이라고 생각했다. – pmk

+0

아마도 NSLog()를 사용하지 않아도된다. (필자는 어쨌든 프로덕션 코드에서 NSLog()를 사용하지 않고 자체 로깅 코드를 사용합니다.) – trojanfoe

+0

NSLog()를 사용하지 않는 이유는 무엇입니까? 나는 항상 NSLog()를 사용 해왔다. – pmk