'SwiftUI: How can I add a new component to the list? Idea: Click the "plus"-button, enter a "Text", store it and... it's been added to the List

So here is the declaration of the variable (passwordNewName):

@State var passwordNewName = ""

It is being updated from the input of a TextField which I coded to accept user's data.

So the idea is that the String which is stored in the variable will - eventually - get transmitted into this List():
struct "Lernset" has the function "all() -> [Lernset]" and the List takes the data from it

If I understand it right, the .setname files are stored inside this static func all() database or whatever these [*braces*] are: enter image description here

Basically, the String-variable passwordNewName should somehow be added to these [braces] automatically... I am really so lost.((

Thank you in advance!!!



Solution 1:[1]

According to the question it looks that you want to update the list along with to send/save it permanently. The actual solution for this problem is to use ObservableObject class with @Published property. But the code below will give you more understanding of scenario.

enter image description here

import SwiftUI

struct ContentView: View {

    
    @State private var lernSet_array: [Lernset] = Lernset.get_all()
    
    var body: some View {
        
        
        VStack(spacing: 0) {
            
            HStack {
                Text("Collection")
                Button {
                    lernSet_array.append(Lernset(setname: "Testing", color: "black"))
                } label: {
                    Image(systemName: "plus")
                        .resizable()
                        .frame(width: 30, height: 30)
                        .padding()
                }
                
            }
            
            ScrollView {
                VStack(alignment: .leading) {
                    ForEach(lernSet_array) { lernset in
                        HStack {
                            Image(systemName: "folder")
                                .resizable()
                                .frame(width: 80, height: 80)
                                .foregroundColor(.gray)
                            Text(lernset.setname)
                        }
                        
                    }
                }
            }
            
        }.onChange(of: lernSet_array) { newValue in
//            Update this database, and somehow store the data
//            and save it permanently… even after restarting the phone
            print("Update this database, and somehow store the data and save it permanently… even after restarting the phone")
            print(lernSet_array.count)
            
        }
        
    }
    
}


struct Lernset: Identifiable, Equatable {
    let id = UUID()
    let setname: String
    let color: String
    
    static func get_all() -> [Lernset] {
        return [Lernset(setname: "Biology - Tail", color: "green"),
                Lernset(setname: "Math - Tail", color: "blue"),
                Lernset(setname: "Phy - Tail", color: "black"),
        ]
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Qazi Ammar