Ziewvater Ziewvater - 28 days ago 8
Swift Question

Implementing debugDescription for DebugPrintable in Swift

I'm trying to write my own debug description for a Swift class. My code looks like the following:

class SceneData : DebugPrintable {
var fileName : String
var scene : Scene
var uuid : String

var debugDescription: String = {
return String(format: "<SceneData: {\n fileName: %s\n scene: %@\n}>", fileName, scene)
}
/*
* More methods, etc.
*/
}


I'm getting the error
'SceneData.Type' does not have a member named 'fileName'
, but
SceneData
very clearly does have a variable
fileName
declared above. I'm also having similar issues when trying to use
self
within the string, and believe that this is related.

How can I make references to instance variables/
self
in the
debugDescription
string properly? I am pretty sure it is possible, both because other people have said it is, and because it seems ridiculous to have an object's debug description not have any actual specifics about itself. What am I doing wrong here?

Answer

remove =, because it should be a computed property.

var debugDescription: String{
        return String(format: "<SceneData: {\n  fileName: %s\n  scene: %@\n}>", fileName, scene)
}