I'm struggling to do a simple append in SwiftUI. Here's my code:
// This is defined in my custom view
var newClass = Class()
// This is inside a List container (I hid the Button's content because it doesn't matter)
Button(action: {
self.newClass.students.append(Student())
print(self.newClass.students) // This prints an Array with only one Student() instance - the one defined in the struct's init
})
// These are the custom structs used
struct Class: Identifiable {
var id = UUID()
@State var name = ""
@State var students: [Student] = [Student()] // Right here
}
struct Student: Identifiable {
var id = UUID()
@State var name: String = ""
}
I think it might be somehow related to the new @Struct thing, but I'm new to iOS (and Swift) development, so I'm not sure.
Class?