ios - 没有成员名称 'subscript' - SWIFT

标签 ios xcode swift

我正在构建一个问答应用程序,并且我已经开设了一个创建问题的类(class)。我还制作了一个将在 viewDidLoad() 时加载的函数。

 class Question{
    var quizQuestion : String = ""
    var firstAnswer : String = ""
    var secondAnswer : String = ""
    var thirdAnswer : String = ""
    var fourthAnswer : String = ""
    var rightAnswer : Int = 0

init(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int)
{

    self.quizQuestion = question
    self.firstAnswer = answerOne
    self.secondAnswer = answerTwo
    self.thirdAnswer = answerThree
    self.fourthAnswer = answerFour
    self.rightAnswer = correctAnswer
    }
}


let one = Question(question: "What was the first planet to be discovered using a telescope, in 1781?", answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)

我的 initialState() 函数

 func initialState() {
    var firstQuestion: AnyObject = arrayOfQuestions[0]

    currentQuestion = 0



    questionLabel.text = "\(firstQuestion[0])"

    //Setting the buttons text
    firstAnswer.setTitle("\(firstQuestion[1])", forState: UIControlState.Normal)
    secondAnswer.setTitle("\(firstQuestion[2])", forState: UIControlState.Normal)
    thirdAnswer.setTitle("\(firstQuestion[3])", forState: UIControlState.Normal)
    fourthAnswer.setTitle("\(firstQuestion[4])", forState: UIControlState.Normal)
}

当我想将按钮文本设置为问题和答案的索引时,它说“问题”没有名为“下标”的成员,这对我来说毫无意义。当我运行该应用程序时,所有内容都设置为“nil”。

有人可以帮忙吗? 请解释你的答案,因为我正在努力学习语言和错误。

最佳答案

questionLabel.text = "\(firstQuestion.quizQuestion)"

//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion.firstAnswer)", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion.secondAnswer)", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion.thirdAnswer)", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion.fourthAnswer)", forState: UIControlState.Normal)

关于ios - 没有成员名称 'subscript' - SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747216/

相关文章:

ios - 如何以编程方式更改 UIView 的 "origin"(如 Xcode 中的标记)?

ios - tableview 数据没有 swift 显示

ios - 控制台中的 Firebase Xcode 输出

ios - swift presentViewController 以编程方式在 destinationVC VDL 中找到 nil

ios - iCloud 用于保存相当频繁的游戏状态更改

iphone - 如何为 iOS 4 编译和使用 CommonCrypto?

iphone - 用于数据的文本文件

swift - 编辑了 xcdatamodeld 的实体,但很快崩溃 - 核心数据

iphone - 调用 NSURLConnectionDataDelegate 方法来下载图像只是偶尔有效

ios - AVAudioEngine.connect 在硬件而非模拟器上崩溃