延迟var给出'实例成员不能用于类型'错误 [英] Lazy var giving 'Instance member can not be used on type' error

查看:129
本文介绍了延迟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

这篇关于延迟var给出'实例成员不能用于类型'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆