Swift “[UILabel]”类型的值在测验应用程序中没有成员“text”
嗨,我正在尝试做一个问答游戏,我得到一个关于UILabel是文本的非成员的错误,这是有意义的,因为choiceText是一个类型[string]Swift “[UILabel]”类型的值在测验应用程序中没有成员“text”,swift,text,uibutton,uilabel,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)
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] }
ChoiceLabel属于ArrayAn类型,UILabels数组没有文本成员,其成员有文本成员。例如,如果要使用forEach访问数组,请遍历数组。错误消息非常清楚。ChoiceLabel[0]。text=firstQuestion.choiceText我已经尝试过了。应该改为:ChoiceLabel[0].text=firstQuestion.choiceText[0]我以前试过,但它说不能将类型[string]签名为类型string。任何建议choiceText也是一个字符串数组。最好使用first从数组中获取第一项:ChoiceLabel.first?.text=firstQuestion.choiceText.first@邓肯:我同意