"实例成员不能在类型和QUOT使用;错误懒财产 [英] "Instance member cannot be used on type" error in lazy property

查看:122
本文介绍了"实例成员不能在类型和QUOT使用;错误懒财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决这个问题,或者告诉我正确的方式来宣告这个属性。

  // MARK:属性
VAR expensiveItems = [项目]()
VAR cheapItems = [项目]()懒惰的VAR allItems:[项目]] = {
    返回[cheapItems,expensiveItems]
}()


解决方案

由于你分配一个封闭的延迟加载属性的结果 - 你需要遵循封规则。其中的一个规则是,如果你指的是 - 你必须明确地做到这一点。 <子>(虽然这并不适用于 @noescape 关闭 - 但你不能在这里反正使用它们)

这仅仅是让你承认你捕捉并保留,但看到它是一个松散加载的属性不应该引起保留周期,它只是调用一次,它的结果,然后分配给属性。

因此​​,你需要使用 self.cheapItems self.expensiveItems

  // MARK:属性
VAR expensiveItems = [项目]()
VAR cheapItems = [项目]()懒惰的VAR allItems:[项目]] = {
    返回[self.cheapItems,self.expensiveItems]
}()

Please help me solve this problem or tell me the right way to declaring this property.

// MARK: Properties
var expensiveItems = [Item]()
var cheapItems = [Item]()

lazy var allItems: [[Item]] = {
    return [cheapItems, expensiveItems]
}()

解决方案

Because you're assigning the result of a closure to your lazily loaded property - you need to follow the closure rules. One of those rules is that if you're referring to self - you must do it explicitly. (although this isn't applicable for @noescape closures – but you can't use them here anyway)

This is simply so that you acknowledge that you're capturing and retaining self, but seeing as it's a lazily loaded property it shouldn't cause a retain cycle, as it is only called once and it's result is then assigned to the property.

Therefore you need to use self.cheapItems and self.expensiveItems.

// MARK: Properties
var expensiveItems = [Item]()
var cheapItems = [Item]()

lazy var allItems: [[Item]] = {
    return [self.cheapItems, self.expensiveItems]
}()

这篇关于&QUOT;实例成员不能在类型和QUOT使用;错误懒财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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