Post

Replies

Boosts

Views

Activity

Reply to SwiftUI View Not Updating on State Change
Observable doesn't work either, at least in my caae. I have an observable object that holds an array of objects, that each hold an array. I can add an object to the nested array and the view will update, but if I add to the first array, it does not, until I quit the app and restart it. Then it shows up!
Topic: UI Frameworks SubTopic: SwiftUI
Nov ’24
Reply to onMove bug
My workaround: // // ContentView.swift // exampleBug work around // // This works by using an order attribute in the Item class and a nextOrder state variable // The nextOrder always starts out as 0, which causes the view to call itself sending the count + 1 of the AllItems array from the query. // If anyone can find a way to use the results of the query without needing to call the view a second time, that would be great. // import SwiftUI import SwiftData @Model final class Item { var timestamp: Date var order: Int var checkbox: Bool = false init(timestamp: Date, order: Int) { self.timestamp = timestamp self.order = order } } struct ContentView: View { @Environment(\.modelContext) private var modelContext @State private var editMode = EditMode.inactive @State var nextOrder = 0 @Query(sort: [ SortDescriptor(\Item.order), ] ) public var AllItems: [Item] var body: some View { if(nextOrder == 0) { ContentView.init(nextOrder: AllItems.count + 1) } else { NavigationStack { List { ForEach(AllItems) { item in HStack { Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) Text(item.order, format: .number) Button("", systemImage: item.checkbox ? "checkmark.circle.fill" : "circle") { item.checkbox.toggle() try? modelContext.save() } } } .onMove(perform: { indices, newOffset in var theItems = AllItems theItems.move(fromOffsets: indices, toOffset: newOffset) var order: Int = 1 theItems.forEach { list in list.order = order order += 1 } }) } .environment(\.editMode, $editMode) .moveDisabled(false) .toolbar { ToolbarItem(placement: .navigationBarTrailing) { EditButton() } ToolbarItem { Button(action: addItem) { Label("Add Item", systemImage: "plus") } } } } } } func addItem() { withAnimation { let newItem = Item(timestamp: Date(), order: nextOrder) self.nextOrder += 1 modelContext.insert(newItem) } } func deleteItems(offsets: IndexSet) { withAnimation { for index in offsets { modelContext.delete(AllItems[index]) } } } } #Preview { ContentView() .modelContainer(for: Item.self, inMemory: true) }```
Topic: UI Frameworks SubTopic: SwiftUI
Nov ’24
Reply to unexpected nil
Sorry, I accidentally accepted the wrong answer. nextOrder was a State var so I didn't think I needed to return it. Since I posted, I've been doing it a different way, and I didn't commit that attempt, so can't go back. It's all related to this: https://developer.apple.com/forums/thread/768948?page=1#814553022 I think the problem is more with Xcode's debugger. It's the least helpful debugger I've ever seen.
Nov ’24
Reply to unexpected nil
I suppose it wasn't so much a question as an example of how bad Xcode has become. The error says it unexpectedly found nil when unwrapping an optional Int that had just been set to an integer. That's not possible! If I need to return a value, the error should have said so. And don't get me started on how it shows errors dozens of lines above where the actual error is! From 2009 to 2014 I used Xcode to write in C++, and it was much better at reporting problems. I know Swift is a relatively new language, but this is not good.
Nov ’24
Reply to Infinite view loop
I thought that I'd solved it by changing the AddItemView toolbar to this: .toolbar { Button("Save") { let tmp = Item(name: name, parent: itemParent) if self.itemParent == nil { context.insert(tmp) } else { itemParent?.children.append(tmp) } try? context.save() dismiss() } } That works for the first layer, which is why the image below has items B and C, but clicking on them causes another infinite loop, this time with the SubItemView calling itself.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’24
Reply to Infinite view loop
It looks like SwiftUI is pre-drawing the navigation links when the screen is loaded, and since one of those links is to the same screen with (usually) a different item, it gets repeated infinitely. If anyone knows how to stop that without having to use a nearly identical view, please post it here.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’24