ARTICLE AD BOX
Let me explain what I am trying to do. If I am doing it wrong let me know how to do it right and forget about this problem. I have a SwiftUI data thing A. I want to have a view that lists the existing As and allows me to add a new one. If I click the EDIT button on one of the existing As edit it. If I want to add a new one I would create an A, but not put it in the SwiftData. I would pass the existing A, or the newly created one, to another view to edit it. The init in the other view moves some fields from the passed A to State variables and displays them for edit. If the user clicks the SAVE button move the State variables back to the A and, if it is an add request, insert the A into the SwiftData.
The problem I have is that I have a state variable of theSimulation. When I try to add add a Simulation I create a new Simulation and set theSimulation equal to it and pass it to the invoked view. In the invoked view the Simulation I get is the one from when I originally created the State variable and not the one I created to add.
I have a SwiftData app. I have a class, Simulation, that is marked @data. In my view I have a query that creates a var simulations that is an array of Simulation. I have @State var theSimulation: Simulation = Simulation(title: "bad",..). In the toolbar for the view I have a button and in that button I have theSimulation = Simulation(title: "",..). I also have a Bool State var of showingAddScreen that I set to true. I have .sheet(isPresented: $showingAddScreen) { AddSimulationView(xsimulation: theSimulation, new: true) } to pop the edit/add view.
My problem is the edit/add view gets the Simulation with the title of bad even though I set the simulation to the one with the blank title. I have put prints in the app when is set theSimulation in the add, the title is blank, and I have a print in the edit/add and it shows bad for the title.
Here is the output from the prints
toolbar ''
init bad
Here is all my code if you need to look at it.
import Foundation import SwiftData @Model class Simulation { public var id: UUID public var title: String public var decks: Int public var players: Int public var playerPosition: Int public var shufflePercent: Int // the percentage of cards left in the show when you shuffle // 100 will cause it to shuffle every hand. // stats public var rounds: Int64 = 0 public var playerHands: Int64 = 0 public var playerWins: Int64 = 0 public var playerLosses: Int64 = 0 public var playerPushes: Int64 = 0 public var playerBlackjacks: Int64 = 0 public var restHands: Int64 = 0 public var restWins: Int64 = 0 public var restLosses: Int64 = 0 public var restPushes: Int64 = 0 public var restBlackjacks: Int64 = 0 init(title: String, decks: Int, players: Int, playerPosition: Int, shufflePercent: Int) { self.id = UUID() self.title = title self.decks = decks self.players = players self.playerPosition = playerPosition self.shufflePercent = shufflePercent } } import SwiftData import SwiftUI struct ContentView: View { @Environment(\.modelContext) var modelContext //@Query var simulations : [Simulation] @Query(sort: \Simulation.title) var simulations : [Simulation] @State var theSimulation: Simulation = Simulation(title: "bad", decks: 6, players: 8, playerPosition: 1, shufflePercent: 30) @State var showingAddScreen = false var StartNanoseconds: Int var body: some View { NavigationStack { List { ForEach(simulations) { simulation in NavigationLink(value: simulation) { Text(simulation.title) } Button("Edit") { } } } .navigationTitle("Blackjack Simulator") .navigationDestination(for: Simulation.self) { simulation in RunSimulationView(simulation: simulation) } .toolbar { ToolbarItem(placement: .automatic) { Button("Add Simulation", systemImage: "plus") { theSimulation = Simulation(title: "", decks: 3, players: 8, playerPosition: 1, shufflePercent: 30) showingAddScreen.toggle() print ("toolbar '\(theSimulation.title)' ") } } } .sheet(isPresented: $showingAddScreen) { AddSimulationView(xsimulation: theSimulation, new: true) } } } init() { let now = Date() let dateComponents = Calendar.current.dateComponents([.nanosecond], from: now) StartNanoseconds = dateComponents.nanosecond! } } import SwiftUI struct AddSimulationView: View { @Environment(\.modelContext) var modelContext @Environment(\.dismiss) var dismiss @State private var title: String @State private var decks: Int @State private var players: Int @State private var playerPosition: Int @State private var shufflePercent: Double @State private var newSimulation: Bool @State private var xsimulation: Simulation var body: some View { NavigationStack { Form { Section { TextField("Title of Simulation", text: $title) Text("Number of Decks") Stepper("\(decks)", value: $decks, in: 1...10) Text("Number of Players") Stepper("\(players)", value: $players, in: playerPosition...10) Text("Player Position") Stepper("\(playerPosition)", value: $playerPosition, in: 1...players) Text("Shuffle percent") Slider(value: $shufflePercent, in: 0...100, step: 1) Text ("Shuffle at \(shufflePercent)%)") } Section { HStack { Spacer() Button("Cancel") { dismiss() } Button ("Save") { xsimulation.title = title xsimulation.decks = decks xsimulation.players = players xsimulation.playerPosition = playerPosition xsimulation.shufflePercent = Int(shufflePercent) if newSimulation { modelContext.insert (xsimulation) } dismiss() } Spacer() } } } .navigationTitle("Add Simulation") } } init(xsimulation: Simulation, new: Bool) { title = xsimulation.title decks = xsimulation.decks players = xsimulation.players playerPosition = xsimulation.playerPosition shufflePercent = Double(xsimulation.shufflePercent) newSimulation = new self.xsimulation = xsimulation print ("init \(xsimulation.title)") } }