import Foundation
struct Person: Identifiable{
let id = UUID()
var name: String
var age: Int
var job: String
var myPerson: Bool
}
class Model: ObservableObject {
@Published var people: [Person] = []
@Published var myPeople: [Person] = []
init(){
addPeople()
}
func addPeople(){
people = peopleData
myPeople = peopleData.filter { $0.myPerson }
}
}
var peopleData = [
person(name: "Bob", age: 22, job: "Student", myPerson: false),
person(name: "John", age: 26, job: "Chef", myPerson: false)
]
I edit the question and my arrays are @Published already. Should I chance peopleData directly or I am doing mistake another where.
import SwiftUI
struct ContentView: View {
@StateObject var model = Model()
var body: some View {
VStack {
VStack {
ForEach(model.myPeople) { person in
VStack(alignment: .leading){
Text("Name: \(person.name)")
Text("Age: \(person.age)")
Text("Age: \(person.job)")
}.padding()
}
}
VStack {
ForEach(model.people) { person in
VStack(alignment: .leading){
Text("Name: \(person.name)")
Text("Age: \(person.age)")
Text("Age: \(person.job)")
}.padding()
}
}
Button("Click") {
// In this part i am tryn to use.
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
This is my SwiftUI view but I can't understand which section I am gonna use sorry about that.
Should I add all class and structs in this view ?
Sorry about that I am kind of new on swift and can't find good resource anywhere. I checked documentations too but I understand nothing.