Created
June 9, 2022 20:21
-
-
Save pwaldhauer/17943eb24bd60e7789bf120c14a36bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Folgende Datenstruktur | |
struct GroupedEntryList: Identifiable { | |
let id = UUID() | |
var label: String | |
var items: [JournalEntry] | |
} | |
// Ein ViewModel, kopierte ich aus irgendeinem Tutorial, keine Ahnung, ob man das braucht | |
// Da ist aber offensichtlich das Published drin und es ist ein Observable Object | |
class GroupedEntriesViewModel: ObservableObject { | |
@Published var groupedEntries: [GroupedEntryList] = [] | |
} | |
// Irgendwo will ich dann folgendes machen: | |
// Die bestehende Gruppe anhand des labels finden | |
var existingGroup = groupedViewModel.groupedEntries.first { group in | |
return group.label == date | |
} | |
// Wenn sie existiert, an die items etwas neues dran hängen | |
if existingGroup != nil { | |
existingGroup!.items.append(entry) | |
} | |
// Das geht aber nicht, oder zumindest wird die UI nicht geupdated, oder kA. | |
// Was geht ist sowas: | |
// Array-Index holen | |
let groupIdx = viewModel.groupedEntries.firstIndex(where: { group in | |
return group.label == date | |
}) | |
if let index = groupIdx { | |
// Entsprechenden Wert im array direkt modifizieren | |
viewModel.groupedEntries[index].items.append(entry) | |
return | |
} | |
// Warum geht das, aber das andere nicht? Fühlt sich an, als würde .first() eine Kopie des objektes herausgeben. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment