我正在尝试为 Swift 类编写自己的调试描述。我的代码如下所示:
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.
*/
}
我收到错误消息 'SceneData.Type' does not have a member named 'fileName'
,但是 SceneData
很明显确实有一个变量 fileName
上面声明的。尝试在字符串中使用 self
时,我也遇到了类似的问题,我相信这是相关的。
如何在 debugDescription
字符串中正确引用实例变量/self
?我很确定这是可能的,因为 other people have said it is ,并且因为让对象的调试描述没有关于自身的任何实际细节似乎很荒谬。我在这里做错了什么?
最佳答案
删除 =
,因为它应该是一个计算属性。
var debugDescription: String{
return String(format: "<SceneData: {\n fileName: %s\n scene: %@\n}>", fileName, scene)
}
关于ios - 在 Swift 中为 DebugPrintable 实现 debugDescription,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31867587/