import UIKit
var options = [String]()
var correctAns = Int()
var question : String
class quizController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
init() {
question = "What quiz are you taking?"
options = ["Medical", "Bollywood", "Math", "Trivia"]
correctAns = 0
}
struct shuffle {
var readyToAskQuestions : [quizController] {
var questions = Array(arrayLiteral: question)
questions.count = questionNum
questions.shuffleInPlace()
return questions
}
init() {
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我在全局var问题中遇到错误:需要初始化的字符串行(全局' var'声明需要初始化表达式或getter / setter说明符)。
但是,如果我尝试初始化它,我会在结构中出现几个错误:
我如何重构我的代码,以便初始化全局var而不会遇到这些错误。
答案 0 :(得分:1)
questions
是String
的数组。 Swift数组没有名为shuffleInPlace
的方法。readyToAskQuestions
quizController
数组类型。它期望[quizController]作为返回值,而不是返回[String],这是错误的。好像你对Swift很新。在这个答案中无法让你清楚所有这些事情。强烈建议阅读Apple swift编程电子书。