Swift Error “static member cannot be used on insta

2019-07-26 12:01发布

问题:

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
    }
}

回答1:

It is an obvious error. Static member can not be accessed with Instance variables like l and r.

Static members must be accessed through the type (class/struct/enum) name like:

GlobalSettings.rating