somu
(somu)
April 13, 2026, 6:22am
1
Hi
Overview
I have a custom type Car for which I have implemented CustomDebugStringConvertible
I have used new line character in debugDescription
For individual elements it seems ok
Problem
While printing an array of cars ([Car]) the formatting is not correct
Expected output
Works with individual elements
Works with array elements with new elements beginning in a new line, like Car B and Car C beginning in a new line
Questions
How can I fix this for array while still working with individual elements?
Is there a better solution?
How is it done for other types that use new line characters?
Example:
struct Car: CustomDebugStringConvertible {
let id: Int
let name: String
let parts: [String]
var debugDescription: String {
"""
Car: id: \(id) name: \(name)
\(parts.map { " - " + $0 }.joined(separator: "\n"))
"""
}
}
let c1 = Car(id: 1, name: "Car A", parts: ["Gearbox", "Wheels", "Doors"])
let c2 = Car(id: 2, name: "Car B", parts: ["Gearbox", "Doors"])
let c3 = Car(id: 3, name: "Car C", parts: ["Wheels", "Doors"])
let cars = [c1, c2, c3]
print(c1)
print("=======")
print(cars)
Output
Car: id: 1 name: Car A
- Gearbox
- Wheels
- Doors
=======
[Car: id: 1 name: Car A
- Gearbox
- Wheels
- Doors, Car: id: 2 name: Car B
- Gearbox
- Doors, Car: id: 3 name: Car C
- Wheels
- Doors]
ibex
April 13, 2026, 7:24am
2
I think the last item in the joined parts is missing the newline character at the end.
As a remedy, you could just append a newline character.
struct Car: CustomDebugStringConvertible {
let id: Int
let name: String
let parts: [String]
var debugDescription: String {
let v = parts.map { " - " + $0 }
let tail = v.joined (separator: "\n")
let u = """
Car: id: \(id) name: \(name)
\(tail)
"""
return u + "\n"
}
}
But then you would have to deal with the extra newline character if it matters when printing individual cars.
1 Like
somu
(somu)
April 13, 2026, 7:40am
3
Thanks @ibex
Not sure if I am nitpicking, yeah the new line character while printing individual Car is not ideal.
Just curious how it is done for other cases, like for example Date uses newline characters but works well in an array or when a single date is printed
somu
(somu)
April 14, 2026, 1:45am
4
How is the formatting done for types like Date which use new line character when printed?
Or is there a better approach to solve this?
xwu
(Xiaodi Wu)
April 14, 2026, 4:40am
5
somu:
I have used new line character in debugDescription
For individual elements it seems ok
Problem
While printing an array of cars ([Car]) the formatting is not correct
Yes, we encountered this problem in the standard library too. And our best solution was...not using line breaks:
A substantial portion of the feedback surrounded the format of the proposed debug description, including a thoughtful point that line breaks could negatively affect existing logging tools. While the exact format of the debugging output is subject to change without review, the language steering group agrees that it is best to omit line breaks in this case to avoid unnecessary breakage since it is not essential for these particular conformances. We encourage the proposal implementor to engage with interested community members in iterating on the output for a concise and readable single-line format.
However, it would not be feasible to add retroactive semantic requirements for CustomDebugStringConvertible across the board banning certain characters, nor did we achieve consensus to adopt a policy for the standard library going forward. Indeed, while existing practice tends to escape line breaks in the debug description (e.g., for String), this is not always the case (e.g., for C++ types). But all can agree that it would make sense not to create known problems for existing tools where just as easily avoided.
2 Likes
somu
(somu)
April 14, 2026, 5:38am
6
Thanks a lot @xwu , I thought I was the only one facing this.
I was ignorantly hoping there would be a way to replace the separator to \n while printing (print / os log) the sequence.