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
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
Replies
Boosts
Views
Activity
Nov ’24
Reply to SwiftUI View Not Updating on State Change
Scratch that. I was passing a new instance of the object to the add view. It's odd that it got added to the data correctly, but my code is working.
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
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
Replies
Boosts
Views
Activity
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.
Replies
Boosts
Views
Activity
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.
Replies
Boosts
Views
Activity
Nov ’24
Reply to Files Missing after Creating Branch
In Xcode, press command-2 and then click Repositories in the window on the left. What branch(es) do you see?
Replies
Boosts
Views
Activity
Nov ’24
Reply to Files Missing after Creating Branch
What happens if you switch to main? (Right click on it and choose switch... ) Also, if you click on the folder at the left above the word "Changes", do you see files?
Replies
Boosts
Views
Activity
Nov ’24
Reply to Files Missing after Creating Branch
What if you click the circled folder?
Replies
Boosts
Views
Activity
Nov ’24
Reply to Files Missing after Creating Branch
What files are missing? It looks like you have the beginnings of a project there. Certainly more than the boilerplate that you get when you create a new project.
Replies
Boosts
Views
Activity
Nov ’24
Reply to Files Missing after Creating Branch
It won't show up in the project or the Finder. Try using Terminal: cd /path/to/project/.git ls
Replies
Boosts
Views
Activity
Nov ’24
Reply to Xcode Cloud has a build, TestFlight doesn't see one
I found a video showing that you need to archive from Xcode. Why that wasn't the first thing you see when deploying, I'll never understand. Now, I'm trying to get my wife's phone to be a test user, but it's an old phone using IOS 15, so it's crashing when trying to accept iCloud terms and conditions. Such fun!
Replies
Boosts
Views
Activity
Nov ’24
Reply to Linked list?
Gah! It's because I don't have a query in the view. It's just taking data from the caller.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’24
Reply to Infinite view loop
It wasn't the query, it was the children array in the Item model competing with the query.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Nov ’24