Swift错误“不能在类型的实例上使用静态成员”。 [英] Swift Error "static member cannot be used on instance of type"
本文介绍了Swift错误“不能在类型的实例上使用静态成员”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使结构的运算符重载,但我收到消息静态成员rating无法在类型GlobalSettings的实例上使用。
我已经读过几个关于此错误的答案,但那里的解决方案根本无法帮助我。我该如何解决这个问题?
I want to overload the operator for my struct but I get the message "static member 'rating' cannot be used on instance of type 'GlobalSettings'". I already read couple answers to this error but the solutions there don't help me at all. How can I solve this problem?
struct GlobalSettings{
static var rating = false
}
func ==(l: GlobalSettings, r: GlobalSettings) -> Bool {
if l.rating == r.rating {
return true
}else{
return false
}
}
推荐答案
不能使用实例变量(如)访问静态成员l
和 r
。
Static members can not be accessed with Instance variables like l
and r
.
静态成员必须通过类型( class
/ struct
/ 枚举
)名称,例如:
Static members must be accessed through the type (class
/struct
/enum
) name like:
GlobalSettings.rating
这篇关于Swift错误“不能在类型的实例上使用静态成员”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文