GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.