Post

Replies

Boosts

Views

Created

Can't append struct to array attribute of struct
I'm writing a simple app to help keep track of notes and such for my job: running facebook ads for different proselyting missions of a church, and I've encountered a problem. The update_campaign function adds the new instance of Campaign to the mcs array, but for some reason it doesn't transfer the new campaign instances to the new_mission.campaigns array. As you can see, the original declaration of the new_mission variable includes a test campaign, which does print out. But, any attempt to add a new Campaign struct to the new_mission.campaigns array doesn't work. Even the little test button above the "Create" button doesn't work. What makes things more confusing is the fact that the mission_leaders array does update and populate on my other view. Any help would be appreciated, I'm still a little new to swift, thanks! Here's my code, I've added notes for you guys in between double asterisks: //window to make new mission struct CreateMission: View { //calls array of all missions @Binding var all_missions: [Mission] //new mission for imput @State private var new_mission = Mission( id: UUID(), name: "", mission_leaders: [], campaigns: [Campaign(id: UUID(), name: "", start_date: Date(), status: .ACTIVE)]) //string arrays for leaders @State private var leader_names_array = [""] @State private var leader_roles_array = [""] @State private var leader_pn_array = [""] //string arrays for campaigns @State private var campaign_names_array = [""] @State private var campaign_dates_array: [Date] = [Date()] @State private var campaign_status_array: [C_status] = [.ACTIVE] var body: some View { VStack{ //name of mission TextField("Mission name:", text: $new_mission.name) //list of mission leaders //ADD DELETE BUTTONS HStack{ VStack{ ForEach($leader_names_array.indices, id: \.self) { index in TextField("Name", text: $leader_names_array[index]) } } VStack{ ForEach($leader_roles_array.indices, id: \.self) { index in TextField("Role", text: $leader_roles_array[index]) } } VStack{ ForEach($leader_pn_array.indices, id: \.self) { index in TextField("Phone Number", text: $leader_pn_array[index]) } } }.frame(width: 500) //add row to list of mission leaders Button(action: { self.leader_names_array.append("") self.leader_roles_array.append("") self.leader_pn_array.append("") }, label: { Text("Add Leader") }) //create campaigns fields HStack{ VStack{ ForEach($campaign_names_array.indices, id: \.self) { index in TextField("Name", text: $campaign_names_array[index]) } } VStack{ ForEach($campaign_dates_array.indices, id: \.self) { index in DatePicker("Select a date", selection: $campaign_dates_array[index], displayedComponents: .date) } } VStack{ ForEach($campaign_status_array.indices, id: \.self) { index in Picker("Select a status", selection: $campaign_status_array[index]) { Text("Acitve").tag(C_status.ACTIVE) Text("Needs Review").tag(C_status.NEEDS_REVIEW) Text("Draft").tag(C_status.DRAFT) Text("Planned").tag(C_status.PLANNED) } } }.frame(width: 200) } //add campaign Button(action: { self.campaign_names_array.append("") self.campaign_dates_array.append(Date()) self.campaign_status_array.append(.ACTIVE) }, label: { Text("Add Campaign") }) Button(action: { new_mission.campaigns.append(Campaign(id: UUID(), name: "Test", start_date: Date(), status: .ACTIVE)) //**this returns an error when I press the button** print(new_mission.campaigns[1].name) }, label: { Text("Test") }) //create mission Button(action: { update_leaders(lna: leader_names_array, lra: leader_roles_array, lpa: leader_pn_array, mls: &new_mission.mission_leaders) update_campaigns(cna: campaign_names_array, cda: campaign_dates_array, csa: campaign_status_array, mcs: &new_mission.campaigns) //**This also returns an error, even when I fill out the text fields and a new Campaign is added to mcs array in the update_campaigns function below** print(new_mission.campaigns[1].name) create_mission(mission: new_mission, all_missions: &all_missions) }, label: { Text("Create") }) } } } //------------------- //---- FUNCTIONS ---- //------------------- func update_leaders(lna: [String], lra: [String], lpa: [String], mls: inout [Leader]) { for index in lna.indices { if lna[index] != "" && lra[index] != "" && lpa[index] != "" { mls.append(Leader( id: UUID(), name: lna[index], role: lra[index], phone_number: lpa[index])) } } } func update_campaigns(cna: [String], cda: [Date], csa: [C_status], mcs: inout [Campaign]) { // Ensure all arrays have the same length guard cna.count == cda.count, cda.count == csa.count else { print("Input arrays must have the same length.") return } for index in cna.indices { let name = cna[index] let date = cda[index] let status = csa[index] // Check for valid data if !name.isEmpty { print("Campaign name not empty") let campaign = Campaign(id: UUID(), name: name, start_date: date, status: status) //**this prints** print(campaign.name) mcs.append(campaign) //**this prints right here, so it's getting to this point** print(mcs[index + 1].name) } else { print("Skipping campaign with empty name.") } } }
1
0
343
Feb ’24