我在记录自定义对象时遇到了一些麻烦。
DateObject 类的示例:
标题
@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;
执行:
#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
现在我像这样设置值:
DateObject *date = [[[DateObject alloc] init] autorelease];
date.month = @"Mar";
date.day = @"04";
date.year = @"2013";
用这个记录对象
NSLog(@"{\n\tMonth:\t%@\n\tDay:\t%@\n\tYear:\t%@\n}",date.month, date.day, date.year);
结果(如预期)在
2013-03-04 10:42:08.821 LoggingCustomObjectsExample[4389:c07] {
Month: Mar
Day: 04
Year: 2013
}
现在尝试用
NSLog(@"%@", date);
我希望我的description
方法被调用(它实际上被调用),但结果总是未格式化:
2013-03-04 10:59:18.835 LoggingCustomObjectsExample[4389:c07] DateObject: "{\n\tMonth:\tMar\n\tDay:\t04\n\tYear:2013}";
我不明白为什么转义序列在这里不起作用。我错过了什么吗?