UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.