我是一名遵循 iOS 编程的新程序员:The Big Nerd Ranch Guide Chapter 2 - Gold Challenge。
我有一堂课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\"
)"
)
感谢所有帮助!谢谢!