我试图稍微了解一下合并,并遇到问题,无法解决这个问题:
我有这个数据源
class NoteManager: ObservableObject, Identifiable {
@Published var notes: [Note] = []
var cancellable: AnyCancellable?
init() {
cancellable = $notes.sink(receiveCompletion: { completion in
print("receiveCompletion \(completion)")
}, receiveValue: { notes in
print("receiveValue \(notes)")
})
}
}
在这里使用:
struct ContentView: View {
@State var noteManager: NoteManager
var body: some View {
VStack {
NavigationView {
VStack {
List {
ForEach(noteManager.notes) { note in
NoteCell(note: note)
}
}
...
我可以在这里更改值:
struct NoteCell: View {
@State var note: Note
var body: some View {
NavigationLink(destination: TextField("title", text: $note.title)
...
无论如何-更改值后,我没有收到receiveValue
事件(也已正确反映在ui中)。 receiveValue
仅在设置时最初被调用-是否有其他方法可以接收更新字段的事件?
添加:
struct Note: Identifiable, Codable {
var id = UUID()
var title: String
var information: String
}
答案 0 :(得分:2)
struct ContentView: View {
@ObservedObject var noteManager: NoteManager
struct NoteCell: View {
@Binding var note: Note
ForEach(Array(noteManager.notes.enumerated()), id: \.element) { i, note in
NoteCell(note: $noteManager.notes[i])
}