gpt4 book ai didi

ios - Objective c 自定义对象描述问题

转载 作者:行者123 更新时间:2023-11-28 22:38:40 26 4
gpt4 key购买 nike

我在记录自定义对象时遇到了一些问题。

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}";

我不明白为什么转义序列在这里不起作用。我错过了什么吗?

最佳答案

这是 NSLog 的已知行为,它会转义那些换行符和制表符,因此输出保持在一行上(参见 SO question)。

我想说的是,您无论如何都不希望在 description 输出中使用换行符,因为它们没有任何值(value)。

关于ios - Objective c 自定义对象描述问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15198885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com