我正在制作一个快速的应用程序。这是一个有趣的琐事应用程序,我遇到了一个我以前从未见过的问题。 (我是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 ​​:您的代码不在方法内,您必须将其包装在方法内。检查此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]

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

10-13 05:41