类型的价值' [UILabel]'没有会员'文字'在测验应用程序中

时间:2018-04-20 16:43:24

标签: swift text uibutton uilabel

您好我正在尝试做一个测验游戏我收到一个关于UILabel不是文本成员的错误,因为choiceText是一个类型[string]。

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' 

我首先尝试将其作为按钮,然后我只是在问题选项的顶部添加标签并尝试显示标签,但它仍然无法正常工作。 如果有任何建议我会非常感激。

1 个答案:

答案 0 :(得分:0)

ChoiceLabel在此宣布

 @IBOutlet var ChoiceLabel: [UILabel]!

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

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