iOS Development

ios – Swiftui – Attempting to entry properties from a category and populate them within the button area

Spread the love


I am making an attempt to be taught Swiftui and I need to know easy methods to entry properties from a category and populate them in struct?

So I need to populate my buttons with the properties from the QuestionBank, however I am uncertain how to do that in Swift.

That is what I bought thus far:

struct ContentView: View {
    
    @State var reply: String
    let allQuestions = QuestionBank()
    var questionNumber: Int = 0
    var rating: Int = 10
    
    let questionBank = QuestionBank()
    
    var physique: some View {

            ZStack {
            
                Coloration.black.ignoresSafeArea()
                    //.navigationBarBackButtonHidden()
                    //.navigationBarHidden(true)
            
                    VStack {
                        
                
                        Textual content("Rating: (rating)")
                            .foregroundColor(.blue)
                            .fontWeight(.heavy)
                            .font(.system(dimension: 30))
                    
                        
                        Button(motion: {
                           
                        }, label: {
                            Textual content("")
                                .font(.customized("", dimension: 30))
                                .body(width: 300, peak: 50)
                                .background(.purple)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
            
                        Button(motion: {
                           
                           
                        }, label: {
                            Textual content("")
                                .font(.customized("", dimension: 30))
                                .body(width: 300, peak: 50)
                                .background(.purple)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
                        
                        Button(motion: {
                            
                        }, label: {
                            Textual content("")
                                .font(.customized("", dimension: 30))
                                .body(width: 300, peak: 50)
                                .background(.purple)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
                        
                        Button(motion: {
                            
                        }, label: {
                            Textual content("")
                                .font(.customized("", dimension: 30))
                                .body(width: 300, peak: 50)
                                .background(.purple)
                                .foregroundColor(.black)
                                .cornerRadius(12)
                        })
                 
                        }
                    }
                    
                }
            
}

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

That is my QuestionBank class the place I’ve an array of questions

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

    }
    
}

And that is my Query class

class Query {
    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 am fairly new to Swift and can not seem to discover a resolution. Can anybody assist?

Leave a Reply

Your email address will not be published. Required fields are marked *