TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 47 challenge

Forums > SwiftUI

hi, im doing Day 47 Challenge but i have a problem. When i save new activity it wont append to [Activity] on main screen.

//
//  ContentView.swift
//  Learner
//
//  Created by Filip Pokłosiewicz on 31/10/2023.
//

import SwiftUI

struct ContentView: View {

    @State private var showingSheet = false

    @State private var activities: [Activity] = [
        Activity(title: "Nauka języka", description: "Codzienne ćwiczenia słownictwa."),
        Activity(title: "Ćwiczenia fizyczne", description: "Bieganie, rower, siłownia"),
        Activity(title: "Nauka programowania", description: "Swift lub SwiftUI")
    ]

    var body: some View {
        NavigationView {
            List(activities) { activity in
                VStack(alignment: .leading) {
                    Text(activity.title)
                        .font(.headline)
                    Text(activity.description)
                        .font(.subheadline)
                }
            }
            .navigationTitle("Activities")
            .toolbar {
                Button {
                    showingSheet = true

                } label: {
                    Image(systemName: "plus")
                }
            }
            .sheet(isPresented: $showingSheet) {
                AddActivity(activities: Activities())
            }
        }
    }
}

#Preview {
    ContentView()
}
//
//  AddActivity.swift
//  Learner
//
//  Created by Filip Pokłosiewicz on 03/11/2023.
//

import SwiftUI

struct AddActivity: View {

    @ObservedObject var activities: Activities

    @State private var title = ""
    @State private var description = ""
    @Environment(\.dismiss) var dismiss

    var body: some View {
        NavigationView {
            Form {
                TextField("Title", text: $title)
                TextField("Description", text: $description)
                    .padding(.bottom, 200)
            } .navigationTitle("Add your activity")
                .toolbar {
                    Button("Save") {
                        let item = Activity(title: title, description: description)
                        activities.items.append(item)
                        dismiss()
                    }
                }
        }
    }
}

#Preview {
    AddActivity(activities: Activities())
}
import Foundation

class Activities: ObservableObject {
    @Published var items = [Activity]()
}
//
//  ActivityItem.swift
//  Learner
//
//  Created by Filip Pokłosiewicz on 31/10/2023.
//

import Foundation

struct Activity: Identifiable {

    var id = UUID()
    var title: String
    var description: String

}

3      

Because when you activities.items.append(item) it adding it to the items array in the class Activities. Also make a NEW instance of Activities So need to do a few changes.

  1. Change
    @State private var activities: [Activity] = [
      Activity(title: "Nauka języka", description: "Codzienne ćwiczenia słownictwa."),
      Activity(title: "Ćwiczenia fizyczne", description: "Bieganie, rower, siłownia"),
      Activity(title: "Nauka programowania", description: "Swift lub SwiftUI")
    ]

    to

    @StateObject private var activities = Activities()

    Change

    .sheet(isPresented: $showingSheet) {
    AddActivity(activities: Activities())
    }

    to

    .sheet(isPresented: $showingSheet) {
    AddActivity(activities: activities)
    }

    if you want the orginal array of items change class Activities to

    class Activities: ObservableObject {
    @Published var items = [
                Activity(title: "Nauka języka", description: "Codzienne ćwiczenia słownictwa."),
                Activity(title: "Ćwiczenia fizyczne", description: "Bieganie, rower, siłownia"),
                Activity(title: "Nauka programowania", description: "Swift lub SwiftUI")
            ]
    }

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.