6

I have the following method:

- (NSString *)description {
    return [NSString stringWithFormat:@"Attribute %@: %@", name, [values description]];
}

Name is a string, values is an NSArray. I have an NSArray containing several of these objects.

When I print the attribute using NSLog(@"Attribute created: %@", [newAttribute description]); it works fine, and prints this:

2012-12-08 14:38:06.883 DT[25684:303] Attribute created: Attribute color: (
    YELLOW,
    PURPLE
)
2012-12-08 14:38:06.884 DT[25684:303] Attribute created: Attribute size: (
    LARGE,
    SMALL
)

However, if I create a NSMutableArray and place several attribute objects in it, I get this output when I print the array in the same manner:

2012-12-08 14:38:06.887 DT[25684:303] Attributes: (
    "Attribute color: (\n    YELLOW,\n    PURPLE\n)",
    "Attribute size: (\n    LARGE,\n    SMALL\n)",
)

Why does it print the newline character in this context, and how would I prevent it from doing so?

4

2 回答 2

1

看起来这个问题在这里得到了回答:https ://stackoverflow.com/a/5599699/5760384

基本上,就像 Exploring 所说, \n 字符在描述中不起作用,但由于某种原因,回车符起作用。因此,请尝试 \r 。我在 Xcode 7 中的重写描述方法中对此进行了测试,它工作正常。

于 2016-01-08T01:47:10.817 回答
0

这是因为当您使用“描述”方法打印时,数据会转换为字符串,因此不会显示换行符。但是当您使用数组打印时,不会将其内容转换为字符串,因此它会显示换行符。

于 2012-12-28T00:40:17.187 回答