gpt4 book ai didi

ios - 带有准确答案字段的琐事(快速)

转载 作者:行者123 更新时间:2023-11-28 12:51:52 26 4
gpt4 key购买 nike

我正在尝试设置带有文本字段的问题以供回答。只有在输入准确答案后才能显示下一个问题。我收到错误代码“if answerField == answers[currentQuestionIndex]”我相信我需要考虑可以输入的答案。我被卡住了,可以在正确的方向上使用一些帮助。谢谢

@IBAction func answerField(sender: AnyObject) {
for index in 1...5 {

if answerField == answers[currentQuestionIndex] {
++self.currentQuestionIndex
if self.currentQuestionIndex == self.questions.count {
self.currentQuestionIndex = 0
}
}

}
}


let questions: [String] = ["From what is cognac made?", "What is 7+7?", "What is the capital of Vermont?"]
let answers: [String] = ["Grapes", "14", "Montpelier"]

override func viewDidLoad() {
super.viewDidLoad()
currentQuestion.text = questions[currentQuestionIndex]

}

最佳答案

answerField 正如您在此处显示的那样,它不是 UITextField;这是一个功能。这就是错误告诉您的内容:将 AnyObject 作为参数并且不返回任何内容的函数 ((AnyObject)->()) 无法与字符串

我想也许您想做的是为您的答案字段创建一个导出(而不是一个 Action ):

@IBOutlet weak var answerField: UITextField! // Make sure you actually hook this up to your text field in the storyboard.

然后,监听文本字段内容的变化:

override func viewDidLoad()
{
super.viewDidLoad()

answerField.addTarget(
self,
action: #selector(answerChanged(_:)),
forControlEvents: .EditingChanged)

currentQuestion.text = questions[currentQuestionIndex]
}

然后处理对您的答案字段文本的更改:

func answerChanged(sender: AnyObject)
{
if (answerField.text ?? "") == answers[currentQuestionIndex]
{
currentQuestionIndex = currentQuestionIndex + 1 < questions.count ? currentQuestionIndex + 1 : 0
currentQuestion.text = questions[currentQuestionIndex]
}
}

或者类似的东西。

关于ios - 带有准确答案字段的琐事(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36289911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com