ios - 如何在 -(NSString *)description 方法中显示数组的换行符\n?

标签 ios objective-c nsstring nsmutablearray

我是一名新程序员,正在学习《iOS 编程:大 Nerd 牧场指南》第 2 章 - 黄金挑战。

我有一个类BNRItem。以及 BNRItem 的子类 BNRContainer

我的 BNRContainer 包含 11 个项目。前十项是 BNRItems。第 11 项是另一个 BNRContainer,也包含 10 项。

在main.m中:

NSLog(@"%@", container);

在 BNRContainer.m 中:

- (NSString *)description
{
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@: Total Value = $%d: %@", [self containerName], [self valueInDollars], subItems];
    return descriptionString;
}

subItems 是一个指向 NSMutableArray 的指针,它包含我想要打印的所有 BNRItems:

@interface BNRContainer : BNRItem
{
    NSString *containerName;
    NSMutableArray *subItems;
}

打印出来的内容:

My Container contains 11 items
My Container: Total Value = $1096: (
    "Undead Goblin (8Q2U8): Worth $73, recorded on 2013-04-21 13:17:54 +0800",
    "Flaming Troll (5Y2V3): Worth $40, recorded on 2013-04-21 13:17:54 +0800",
    "Undead Human (2F9Z7): Worth $40, recorded on 2013-04-21 13:17:54 +0800",
    "Flying Elf (8G5V6): Worth $99, recorded on 2013-04-21 13:17:54 +0800",
    "Undead Orc (3P9B1): Worth $10, recorded on 2013-04-21 13:17:54 +0800",
    "Undead Goblin (6R5C1): Worth $93, recorded on 2013-04-21 13:17:54 +0800",
    "Stone Giant (3E4O0): Worth $1, recorded on 2013-04-21 13:17:54 +0800",
    "Stone Goblin (3A6T4): Worth $30, recorded on 2013-04-21 13:17:54 +0800",
    "Shiny Troll (8S3I1): Worth $77, recorded on 2013-04-21 13:17:54 +0800",
    "Flying Orc (4F6F9): Worth $65, recorded on 2013-04-21 13:17:54 +0800",
    "Dublicate Container: Total Value = $568: (\n    \"Flying Elf (1P5F4): Worth $29, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Shiny Elf (3R2Q6): Worth $88, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Shiny Dwarf (4X7P8): Worth $38, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Undead Goblin (7E4L1): Worth $77, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Flying Human (8K9Y2): Worth $35, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Flying Human (8J8T0): Worth $94, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Shiny Dwarf (5E2Z1): Worth $42, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Flying Troll (1K1G7): Worth $71, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Shiny Troll (9X6C8): Worth $40, recorded on 2013-04-21 13:17:58 +0800\",\n    \"Flying Elf (3H0R6): Worth $54, recorded on 2013-04-21 13:17:58 +0800\"\n)"

)

如何让它像这样打印出\n?

My Container contains 11 items
My Container: Total Value = $1096: (
    "Undead Goblin (8Q2U8): Worth $73, recorded on 2013-04-21 13:17:54 +0800",
    "Flaming Troll (5Y2V3): Worth $40, recorded on 2013-04-21 13:17:54 +0800",
    "Undead Human (2F9Z7): Worth $40, recorded on 2013-04-21 13:17:54 +0800",
    "Flying Elf (8G5V6): Worth $99, recorded on 2013-04-21 13:17:54 +0800",
    "Undead Orc (3P9B1): Worth $10, recorded on 2013-04-21 13:17:54 +0800",
    "Undead Goblin (6R5C1): Worth $93, recorded on 2013-04-21 13:17:54 +0800",
    "Stone Giant (3E4O0): Worth $1, recorded on 2013-04-21 13:17:54 +0800",
    "Stone Goblin (3A6T4): Worth $30, recorded on 2013-04-21 13:17:54 +0800",
    "Shiny Troll (8S3I1): Worth $77, recorded on 2013-04-21 13:17:54 +0800",
    "Flying Orc (4F6F9): Worth $65, recorded on 2013-04-21 13:17:54 +0800",
    "Dublicate Container: Total Value = $568: (
        "Flying Elf (1P5F4): Worth $29, recorded on 2013-04-21 13:17:58 +0800\",
        "Shiny Elf (3R2Q6): Worth $88, recorded on 2013-04-21 13:17:58 +0800\",
        "Shiny Dwarf (4X7P8): Worth $38, recorded on 2013-04-21 13:17:58 +0800\",
        "Undead Goblin (7E4L1): Worth $77, recorded on 2013-04-21 13:17:58 +0800\",
        "Flying Human (8K9Y2): Worth $35, recorded on 2013-04-21 13:17:58 +0800\",
        "Flying Human (8J8T0): Worth $94, recorded on 2013-04-21 13:17:58 +0800\",
        "Shiny Dwarf (5E2Z1): Worth $42, recorded on 2013-04-21 13:17:58 +0800\",
        "Flying Troll (1K1G7): Worth $71, recorded on 2013-04-21 13:17:58 +0800\",
        "Shiny Troll (9X6C8): Worth $40, recorded on 2013-04-21 13:17:58 +0800\",
        "Flying Elf (3H0R6): Worth $54, recorded on 2013-04-21 13:17:58 +0800\"
    )"
)

感谢所有帮助!谢谢!

最佳答案

打印日志时,可以在打印内容中添加\n\r。 例如

NSLog(@"This is the first Line \n\r This is the Second Line");

将打印:

这是第一行 这是第二行

因此,请尝试使用此功能来随时打破界限。

关于ios - 如何在 -(NSString *)description 方法中显示数组的换行符\n?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16128331/

相关文章:

ios - UITextField 在数组中找到最匹配的字符串

ios - 如何将 NSString 从字符串值转换为 ASCII 值

ios - 开始编辑 UISearchbar 会更改 UINavigationbar 颜色

ios - 如何在 Xcode 的 UI 测试中检查是否存在从网络显示的静态文本?

arrays - 将数组转换为 NSAttributedString

ios - 检测 iPhone 6 及 plus

nsmutable 数组上的 iphone sdk 搜索栏

ios - 如何在 Unicode 中用字符下标?

javascript - 使用 modernizr 检测 iOS 设备

ios - 使用 AVFoundation 像在屏幕上一样捕获图像预览