My model Keyword
has the following implementation:
public struct Keyword: Codable, Equatable, Identifiable {
public var id: String {
name
}
public var name: String
public var popularity: Int?
public static func == (lhs: Keyword, rhs: Keyword) -> Bool {
return lhs.name == rhs.name
}
[...]
KeywordsView
has the following implementation:
@ObservedObject var viewModel = KeywordsViewModel()
var body: some View {
NavigationView {
List {
ForEach(viewModel.keywords) { keyword in
KeywordView(keyword: keyword)
}
[...]
When popularity
is updated (e.g. using a web API), the view never gets updated. It works fine when I remove the static ==
method. It also works fine if I make this method always return false
.
I have an idea why this is happening: SwiftUI probably (implicitly) uses ==
to figure out whether the view needs to be updated. And because popularity
is left out of the equation, SwiftUI never updates the view although popularity
is changed.
However, I would like to use the custom ==
implementation to make use of, amongst others, contains(_:)
and compare instances of Keyword
in my way.
How can I make this work?