ViewController.Type没有成员名称'answerOne' - SWIFT [英] ViewController.Type does not have a member names 'answerOne' - SWIFT
问题描述
我正在制作一个快速的应用程序。这是一个有趣的琐事应用程序,我遇到了一个我以前没见过的问题。 (我是swift和ios开发错误的新手)
I am making a swift app. It is a trivia app for fun and I have run into an issue that I havent seen before. (I am new to swift and ios development errors)
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'的成员
When I declare the "questionOneArray", it says "TriviaViewController does not have a member named 'answerOne'"
请向我解释这个错误是什么以及我如何解决它。
谢谢。
Please explain to me what this error is and how I can fix it. Thank you.
推荐答案
解决方案1 :您的代码不在方法内,你必须将它包装在方法中。检查此 stackoverflow答案
Solution 1 : Your code is not inside a method, you have to wrap it inside the method. Check this stackoverflow answer
解决方案2 :在 struct
中保留问题和答案
并访问它们,如果你不想将它包装在里面一个函数。
Solution 2 : Keep your questions and answers
inside struct
and access them, if you don't want to wrap it inside a function.
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]
这篇关于ViewController.Type没有成员名称'answerOne' - SWIFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!