如何在 - (NSString *)描述方法中显示数组的换行符?

时间:2013-04-21 05:44:58

标签: ios objective-c nsstring nsmutablearray

我是继iOS编程后的新程序员:大书呆子牧场指南第2章 - 黄金挑战。

我有一个班级BNRItem。以及BNRItemBNRContainer

的子类

我的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的指针,它包含我要打印的所有BNRItem:

@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\"
    )"
)

感谢所有帮助!谢谢!

1 个答案:

答案 0 :(得分:1)

打印日志时,可以添加\ n \ r到您打印的内容。 例如

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

将打印:

这是第一行 这是第二行

因此,请尝试使用此功能随时断开该行。