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

SOLVED: @State var in loop

Forums > SwiftUI

I am creating a number of pickers from an array but i need to capture there state.

I dont know how many state vars i will need nor what they will be called, this will be established dynamically.

How do i create state vars for each of these elements.

struct CandidateReview: View {
    var reviewScore = ["Terrible", "Poor", "Average", "Good", "Excellent"]
    var reviewCriterias = ["Communication", "Time Management", "Work Ethic", "Creativity", "Teamwork"]

    var candidateName = "John Doe"

    @State private var selectedReviewScore = "Average"
    @State private var scoreNotes = ""

    var body: some View {
        NavigationStack{

            Form{
                ForEach(reviewCriterias, id: \.self) { reviewCriteria in
                    Section(header: Text(reviewCriteria)){
                        Picker("\(reviewCriteria) Score", selection: self.$selectedReviewScore){
                            ForEach(reviewScore, id: \.self){
                                Text($0)
                            }
                        }
                        TextField("\(reviewCriteria) Notes...", text: self.$scoreNotes)
                    }
                }
            }
            .navigationTitle(candidateName)
        }

    }
}

   

Hello, don't need to add multiple state vars, just creates an published array and initialized with your data.

Create a Model:

struct ReviewModel {
    var reviewScore: String
    var reviewCrieteria: String
    var scoreNotes: String
}

Create a ViewModel:

class ReviewManager: ObservableObject {
    @Published var reviews: [ReviewModel]
    static var reviewCriterias = ["Communication", "Time Management", "Work Ethic", "Creativity", "Teamwork"]
    var reviewScore = ["Terrible", "Poor", "Average", "Good", "Excellent"]

    init() {
        self.reviews = ReviewManager.getData()
    }

    static func getData() -> [ReviewModel] {
        reviewCriterias.map { criteria in
            ReviewModel(
                reviewScore: "Poor",
                reviewCrieteria: criteria,
                scoreNotes: ""
            )
        }
    }
}

And use them in your view:

struct CandidateReview: View {
    var candidateName = "John Doe"
    @ObservedObject var reviewObj = ReviewManager()

    var body: some View {
        if #available(iOS 16.0, *) {
            NavigationStack{

                Form{
                    ForEach(reviewObj.reviews.indices, id: \.self) { index in
                        Section(header: Text(reviewObj.reviews[index].reviewCrieteria)){
                            Picker("\(reviewObj.reviews[index].reviewCrieteria) Score", selection: $reviewObj.reviews[index].reviewScore){
                                ForEach(reviewObj.reviewScore, id: \.self){
                                    Text($0)
                                }
                            }
                            TextField("\(reviewObj.reviews[index].reviewCrieteria) Notes...", text: $reviewObj.reviews[index].scoreNotes)
                        }
                    }
                }
                .navigationTitle(candidateName)
            }
        } else {
            // Fallback on earlier versions
        }

    }
}

   

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.