objective-c - NSLog 输出中出现换行符

标签 objective-c nsarray nslog

<分区>

我有以下方法:

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

Name 是一个字符串,values 是一个 NSArray。我有一个包含其中几个对象的 NSArray。

当我使用 NSLog(@"Attribute created: %@", [newAttribute description]); 打印属性时,它工作正常,并打印如下:

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
)

但是,如果我创建一个 NSMutableArray 并在其中放置几个​​属性对象,当我以相同的方式打印数组时,我会得到这个输出:

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)",
)

为什么它会在此上下文中打印换行符,我该如何防止它这样做?

最佳答案

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

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

关于objective-c - NSLog 输出中出现换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13781196/

相关文章:

ios - UITableView:在顶部添加单元格时保持选择(单选)?

iphone - Facebook SDK 和 iOS 应用程序 (authButtonAction) 的问题

cocoa - NSLog 10b 含义?

objective-c - 为什么 NSLog 显示错误的 int 和 char?

ios - UIScrollView 卡住了

iphone - HTML 解析因重音字母而失败(例如 : é)

iphone - 为自定义类保存到 nsuserdefaults 的最佳方法?

objective-c - NSArray 中的空值

objective-c - 使用 KVC 跨 n 个键查找最小值

ios - NSLog 的奇怪行为