类型'[UILabel]'的值在测验应用中没有成员'text'


嗨,我

正在尝试做一个问答游戏,我收到一个关于 UILabel 是文本非成员的错误,这是有意义的,因为 choiceText 是一种类型 [字符串]。

import Foundation
class Question {
    let questionText: String
    let choiceText: [String]
    let answer : Int
    init(text: String, choice: [String], correctAnswer: Int){
        questionText = text
        answer = correctAnswer
        choiceText = choice

    }
}

import Foundation
class QuestionBank{
    var list = [Question]()

    init() {
        let QuizItem = Question(text: "What is your name?", choice: 
        ["S","B","D","C"], correctAnswer: 0)
        list.append(QuizItem)
        list.append(Question(text: "What is your favourite cat?", choice: ["Bob","Marley","zed","fer"], correctAnswer: 2))
        list.append(Question(text: "What is your favourite cat?", choice: ["Bob","Marley","zed","fer"], correctAnswer: 0))
import UIKit
class QuizViewController: UIViewController {
    let allQuestions = QuestionBank()
    @IBOutlet weak var QuestionProgress: UILabel!
    @IBOutlet weak var QuestionLabel: UILabel!
    @IBOutlet weak var ScoreLabel: UILabel!
    @IBOutlet var ChoiceLabel: [UILabel]!

    override func viewDidLoad() {
        super.viewDidLoad()
        let firstQuestion = allQuestions.list[0]
        QuestionLabel.text = firstQuestion.questionText
        ChoiceLabel.text = firstQuestion.choiceText //Value of type 
       '[UILabel]' has no member 'text' 

首先尝试将其作为按钮,然后我只是在问题选项顶部添加标签并尝试显示标签,但仍然不起作用。如果有任何建议,我将不胜感激。

ChoiceLabel在这里声明

 @IBOutlet var ChoiceLabel: [UILabel]!

是一个标签数组,如果是集合你必须索引它

(0...ChoiceLabel.count-1).forEach { ChoiceLabel[$0].text = firstQuestion.choiceText[$0] }

相关内容

最新更新