class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
questionLabel.text = questions[currentQuestionIndex]
}
}
var questionLabel : UILabel!
var answerLabel: UILabel!
let questions: [String] = [
"What is 7+7?",
"What is the capital of Vermont?",
"What is cognac made from?",
]
let answers: [String] = [
"14",
"Montpelier",
"Grapes",
]
var currentQuestionIndex: Int=0
...
func showNextQuestion(_ sender: UIButton) {
currentQuestionIndex+=1
if currentQuestionIndex == questions.count {
currentQuestionIndex = 0
}
let question: String = questions[currentQuestionIndex]
questionLabel.text = question
answerLabel.text = "???"
}
func showAnswer(_ sender: UIButton) {
let answer: String = answers[currentQuestionIndex]
answerLabel.text = answer
}
该代码将无法生成。每当我尝试在func前面添加“ IBAction”时,都会说它需要替换。下划线标有func,错误为“运算符后的期望表达式”。接下来的错误是func showNextQuestion行。请,谢谢你
答案 0 :(得分:0)
请检查以下使用您的函数创建的类</ p>
第1类:ViewController
第2类:具有扩展功能的NewController
import UIKit
/// Option 1
class ViewController: UIViewController {
/*
/// Add IBOutlets Here
*/
/// Your Assigned Properties
private var questionLabel : UILabel!
private var answerLabel: UILabel!
private var currentQuestionIndex: Int = 0
let questions: [String] = [
"What is 7+7?",
"What is the capital of Vermont?",
"What is cognac made from?",
]
let answers: [String] = [
"14",
"Montpelier",
"Grapes",
]
override func viewDidLoad() {
super.viewDidLoad()
/// Assign Question To Label
questionLabel.text = questions[currentQuestionIndex]
}
/// Take These Functions Inside Your Class
func showNextQuestion(_ sender: UIButton) {
currentQuestionIndex+=1
if currentQuestionIndex == questions.count {
currentQuestionIndex = 0
}
let question: String = questions[currentQuestionIndex]
questionLabel.text = question
answerLabel.text = "???"
}
func showAnswer(_ sender: UIButton) {
let answer: String = answers[currentQuestionIndex]
answerLabel.text = answer
}
}
/// Option 2
class NewController: UIViewController
{
/*
/// Add IBOutlets Here
*/
/// Your Assigned Properties
private var questionLabel : UILabel!
private var answerLabel: UILabel!
private var currentQuestionIndex: Int = 0
let questions: [String] = [
"What is 7+7?",
"What is the capital of Vermont?",
"What is cognac made from?",
]
let answers: [String] = [
"14",
"Montpelier",
"Grapes",
]
override func viewDidLoad() {
super.viewDidLoad()
/// Assign Question To Label
questionLabel.text = questions[currentQuestionIndex]
}
}
//MARK:- Required Functions
extension NewController {
/// Or Make Separate Extension of Class and Add Functions there
func showNextQuestion(_ sender: UIButton) {
currentQuestionIndex+=1
if currentQuestionIndex == questions.count {
currentQuestionIndex = 0
}
let question: String = questions[currentQuestionIndex]
questionLabel.text = question
answerLabel.text = "???"
}
func showAnswer(_ sender: UIButton) {
let answer: String = answers[currentQuestionIndex]
answerLabel.text = answer
}
}
犯了错误您在类外声明了函数
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
questionLabel.text = questions[currentQuestionIndex]
}
}
/// Here - This is Wrong
正确方法
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
questionLabel.text = questions[currentQuestionIndex]
}
/// Functions Need to Be Here
}