延迟var给出'实例成员不能用于类型'错误

延迟var给出'实例成员不能用于类型'错误

问题描述:

我现在有几次这个错误,我采用了不同的解决方法,但我真的很好奇它为什么会发生。基本场景如下:

I had this error several times now and I resorted to different workarounds, but I'm really curious why it happens. Basic scenario is following:

class SomeClass {
  var coreDataStuff = CoreDataStuff!
  lazy var somethingElse = SomethingElse(coreDataStuff: coreDataStuff)
}

所以我理解我不能在类完全初始化之前使用self,但是在这种情况下我使用自身属性 coreDataStuff 来初始化一个懒惰的var,直到我的实例准备好才会发生。

So I understand I can not use self before class is fully initialised, but in this case I'm using self property coreDataStuff to initialise a lazy var which will not happen until my instance is ready.

任何人都可以解释我为什么我得到
实例成员不能用于类型错误?

Anybody could explain me why I'm getting Instance member can not be used on type error?

试试看:

class SomeClass {
  var coreDataStuff = CoreDataStuff!
  lazy var somethingElse: SomethingElse = SomethingElse(coreDataStuff: self.coreDataStuff)
}

确定lazy var的类型并将 self。添加到您传递的参数

It is important to precise the type of your lazy var and to add self. to the argument you pass