New line character showing up in NSLog output [duplicate]
Asked Answered
S

2

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?

Sympetalous answered 8/12, 2012 at 19:43 Comment(2)
did u find any solution?Dosh
Still no solution? How annoyingSniperscope
P
1

It looks like this question was answered here: https://mcmap.net/q/210614/-nsstring-with-n-or-line-break

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.

Plaudit answered 8/1, 2016 at 1:47 Comment(2)
make reputation to flag a question as duplicate.Jauch
I would go to the reputation broker, borrow some, then use it to flag as a duplicate, then earn reputation then pay back the reputation broker :PPlaudit
M
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.

Mufinella answered 28/12, 2012 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.