String interpolation produces a debug description for an optional value; did you mean to make this explicit?

This makes my head dizzy! Help me out of this peril.

How to avoid this with my own class objects?

let obj: LanguageItem? = LanguageItem(language: "en")
print("object: \(obj)")

struct LanguageItem: Codable
{
    var language: String
    var name: String?
}

extension LanguageItem: CustomStringConvertible {
    var description: String {
        "Lang:\(language) name:\(name ?? "(none)")"
    }
}

The print statement still prints "Optional(Lang:en name:(none))". How to get rid the Optional prefix?

You can use nil coalescing:

print("object: \(obj ?? LanguageItem(language: ""))")

or, less safe, force unwrap:

print("object: \(obj!)")

AFAIK, you cannot get it directly.

What you can do:

if let obj { print("object: \(obj)") } else { print("object: nil") }

Or file a request to Swift.org

String interpolation produces a debug description for an optional value; did you mean to make this explicit?
 
 
Q