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?

HAS
  • 19,140
  • 6
  • 31
  • 53
jstm88
  • 3,335
  • 4
  • 38
  • 55

2 Answers2

1

It looks like this question was answered here: https://stackoverflow.com/a/5599699/5760384

Basically, like Exploring said, the \n character doesn't work in the description, but for some reason a carriage return does. So try \r instead. I tested this out in an overridden description method in Xcode 7 and it works fine.

Community
  • 1
  • 1
Nicronaide
  • 61
  • 1
  • 6
0

It is because while you printing using method "description" then the data is converted into string so it is not showing the newline character. But when you are printing using array then the contents of the are not converted into string so it is showing the newline character.

Exploring
  • 925
  • 6
  • 18