ios – Hello, I’m trying to learn Swiftui and I want to know how do I access properties from a class and populate them in struct


So I want to populate my buttons with the properties from the QuestionBank, but I’m unsure how to do this in Swift.

This is what I got so far:

struct ContentView: View {
    
    @State var answer: String
    let allQuestions = QuestionBank()
    var questionNumber: Int = 0
    var score: Int = 10
    
    let questionBank = QuestionBank()
    
    var body: some View {

            ZStack {
            
                Color.black.ignoresSafeArea()
                    //.navigationBarBackButtonHidden()
                    //.navigationBarHidden(true)
            
                    VStack {
                        
                
                        Text("Score: \(score)")
                            .foregroundColor(.blue)
                            .fontWeight(.heavy)
                            .font(.system(size: 30))
                    
                        
                        Button(action: {
                           
                        }, label: {
                            Text("")
                                .font(.custom("", size: 30))
                                .frame(width: 300, height: 50)
                                .background(.red)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
            
                        Button(action: {
                           
                           
                        }, label: {
                            Text("")
                                .font(.custom("", size: 30))
                                .frame(width: 300, height: 50)
                                .background(.red)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
                        
                        Button(action: {
                            
                        }, label: {
                            Text("")
                                .font(.custom("", size: 30))
                                .frame(width: 300, height: 50)
                                .background(.red)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
                        
                        Button(action: {
                            
                        }, label: {
                            Text("")
                                .font(.custom("", size: 30))
                                .frame(width: 300, height: 50)
                                .background(.red)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
                 
                        }
                    }
                    
                }
            
}

#Preview {
    ContentView(answer: "String")
}

This is my QuestionBank class where I have an array of questions

class QuestionBank {
    
    var questionList = [Question]()
    
    init() {
        questionList.append(Question(displayQuestion: "a", questionText1: "1", questionText2: "2", questionText3: "3", questionText4: "4", correctAnswer: "a"))
        
        questionList.append(Question(displayQuestion: "a", questionText1: "1", questionText2: "2", questionText3: "3", questionText4: "4", correctAnswer: "a"))
        
        questionList.append(Question(displayQuestion: "a", questionText1: "1", questionText2: "2", questionText3: "3", questionText4: "4", correctAnswer: "a"))
        

    }
    
}

And this is my Question class

class Question {
    var displayQuestion: String
    var questionText: [String]
    var correctAnswer: String

    init(displayQuestion: String, questionText1: String, questionText2: String, questionText3: String, questionText4: String, correctAnswer: String) {
        self.displayQuestion = displayQuestion
        self.questionText = [questionText1, questionText2, questionText3, questionText4]
        self.correctAnswer = correctAnswer
    }
    
    func getDisplayQuest() -> String {
            return displayQuestion
        }

        func getQuestionText() -> [String] {
            questionText.shuffle()
            return questionText
        }

        func getCorrectAnswer() -> String {
            return correctAnswer
        }
}

I’m pretty new to Swift and can’t seem to find a solution. Can anyone help?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img