ViewController.Type没有成员名称' answerOne' - SWIFT

时间:2015-01-02 05:22:44

标签: ios xcode swift

我正在制作一个快速的应用程序。这是一个有趣的琐事应用程序,我遇到了一个我以前没见过的问题。 (我是swift和ios开发错误的新手)

import UIKit

class TriviaViewController: UIViewController {

@IBOutlet var myQuestionCounter: UILabel!
@IBOutlet var myQuestion: UILabel!
@IBOutlet var firstAnswer: UIButton!
@IBOutlet var secondAnswer: UIButton!
@IBOutlet var thirdAnswer: UIButton!
@IBOutlet var fourthAnswer: UIButton!



var timer = NSTimer() //TO-DO
var count = 0
var questionCounter = 0

var firstQuestion = "What was the first planet to be discovered using the telescope, in 1781?"
var answerOne = "Mercury"
var answerTwo = "Uranus"
var answerThree = "Venus"
var answerFour = "Jupiter"

var firstCorrectAnswer = 2

//This next line gives the ERROR
let questionOneArray = [answerOne, answerTwo, answerThree, answerFour, firstCorrectAnswer]

}

当我声明“questionOneArray”时,它说“TriviaViewController没有名为'answerOne'的成员”

请向我解释这个错误是什么以及我如何解决它。 谢谢。

1 个答案:

答案 0 :(得分:3)

解决方案1 ​​:您的代码不在方法内,您必须将其包装在方法中。请检查此stackoverflow answer

解决方案2 :如果您不想将其包含在函数中,请将questions and answers保留在struct内并访问它们。

struct firstQuestion {
    static var question = "What was the first planet to be discovered using the telescope, in 1781?"
    static var answerOne = "Mercury"
    static var answerTwo = "Uranus"
    static var answerThree = "Venus"
    static var answerFour = "Jupiter"
    static var correctAnswer = "Correct Answer"
}

var questionOneArray = [firstQuestion.answerOne, firstQuestion.answerTwo, firstQuestion.answerThree, firstQuestion.answerFour, firstQuestion.correctAnswer]