|
Hi, I am new to swiftui, trying to make a chess app that shows a board 8x8 Rectangles. Chess position is given as a dictionary with each square as key a1-8, b1-8...h1-h8 and a letter describing the piece on that square. The dictionary needs to change as moves are made and the board view should update. But I cannot see how to do it. Here is my view (only one line etc shwon for brevity)
I used a controller as various parts of UI will change the position (toolbar buttons, menuitems etc):
In func pieceOverlay if I use a leading $ then I get a compile error:
"Referencing subscript 'subscript(_:)' on 'Binding' requires that 'Position' conform to 'MutableCollection'" If I don't use the $ then code compiles but then when I update the controller's position dictionary the view does not change. I am confused around when I need @State, @Binding, @StateModel or whether I don't need any of them. Any hwelp would be appreciated. Sorry for the length of my question Quartz64 |
|
@Quartz64 has an extraordinary request:
@twoStraws produced a high-quality twenty-three minute video explaining this. I cannot explain in a forum post what he illustrates brilliantly in a twenty-three minute video. His video literally starts with "I am going to answer one of the most common questions people have when using SwiftUI..." See -> Dear Quartz64... Also, are you trying to learn on your own? I sense you are guessing at how to assemble these parts, rather than following a plan? True? Some of your code is, uh, ummmm, rather beginner-ish. For instance, you seem to build your board using brute-force design. SwiftUI is very modular, and could help simplify your design immensely. @twoStraws has a terrific course titled 100 Days of SwiftUI. I think you'd learn a lot from following his course. Learning lessons on sub-views, looping, encapsulation, and the like. Give it a shot! Your chess program will be much easier to code if you care to follow along. Keep CodingGood luck! |
|
I see you use an @ObservedObject for the board controller.
Do you have a @StateObject for the board controller in another view that you pass to the board view? If not, you should use @StateObject for the board controller.
When you use classes for your app's data, the view that owns the object should use @StateObject. If you pass that object to other views, those views should use @ObservedObject. When you use structs for your app's data, the view that owns the object should use @State. If you pass that object to other views, those views should use @Binding. |
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.