Home > Blockchain >  How to create different views by using only one class instance?
How to create different views by using only one class instance?

Time:08-05

I want to create different views by using only one class instance. My goal is not to create a separate instance for each view(group), instead, I would like to pass data through all views. Every time I try to pass data from another view I am faced with "Cannot use instance member within property initializer" and "Generic parameter 'C' could not be inferred" errors. For clarifying my problem I added some example code and marked errors as a comment.

class DataService {
    
    static let group1: [Person] = [
        Person(name: "Jack", age: 21),
        Person(name: "Anna", age: 29),
        Person(name: "Steve", age: 12),
        Person(name: "Carl", age: 16),
    ]
    
    static let group2: [Person] = [
        Person(name: "Crag", age: 19),
        Person(name: "Daniel", age: 29),
        Person(name: "Katty", age: 36),
        Person(name: "Leo", age: 13),
    ]
    
}

struct Person {
    let name: String
    let age: Int
}

class PersonClass: ObservableObject {
    
    @Published var peopleArray: [Person]
    
    init(data: [Person]) {
        self.peopleArray = data
    }
}

struct humanView: View {
    
    let data: [Person]
    
    @StateObject var person = PersonClass(data: data) // Error1: Cannot use instance member 'data' within property initializer
    
    var body: some View {
        
        VStack {
            ForEach(person.peopleArray.indices, id: \.self) { index in // Error2: Generic parameter 'C' could not be inferred
                Rectangle()
                    .fill(Color.black)
                    .frame(width: 120, height: 120)
                    .cornerRadius(10)
                    .overlay(
                        Text(person.peopleArray[index].name)
                            .font(.headline)
                            .foregroundColor(.white)
                    )
            }
        }
    }
}
     
struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink("Group 1") {
                humanView(group: DataService.group1)
            }
            NavigationLink("Group 2") {
                humanView(group: DataService.group2)
            }
        }
    }
}

CodePudding user response:

You should use "@State" variable PersonClass. There is the anwser

class PersonClass {
    var peopleArray: [Person]
    
    init(data: [Person]) {
        self.peopleArray = data
    }
}

struct humanView: View {
    
    @State var personClass: PersonClass
    
    var body: some View {
        
        VStack {
            ForEach(personClass.peopleArray.indices, id: \.self) { index in // Error2: Generic parameter 'C' could not be inferred
                Rectangle()
                    .fill(Color.black)
                    .frame(width: 120, height: 120)
                    .cornerRadius(10)
                    .overlay(
                        Text(personClass.peopleArray[index].name)
                            .font(.headline)
                            .foregroundColor(.white)
                    )
            }
        }
    }
}
     
struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack{
                NavigationLink("Group 1") {
                    humanView(personClass: PersonClass(data: DataService.group1))
                }
                NavigationLink("Group 2") {
                    humanView(personClass: PersonClass(data: DataService.group2))
                }
            }
        }
    }
}
  • Related