我有一个名为 SearchResults 的视图。每当传入的 ObservedObject 发生更改时,它都应该更新,但是 View 没有使用新值更新,我不知道为什么。
import SwiftUI
struct SearchResults: View {
@ObservedObject var VModel: ViewModel
var body: some view {
List {
ForEach(self.VModel.searchResults, id: \.self) { result in
Text(result)
}
}
}
}
class ViewModel: ObservableObject {
@Published var searchResults: [String] = []
func findResults() {
//Do the update to 'searchResults' here
//This function is called at another point in the code in a pretty big file...I think it might make it more confusing to have like a couple hundred more lines of code in here
}
}
旁注:VModel 有一个类型为 [String] 的数组,称为 searchResults,我认为这个视图应该在 searchResults 数组更新时更新..?