gpt4 book ai didi

ios - 从 JSON 加载信息

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:15 24 4
gpt4 key购买 nike

我的 JSON 文件中有 3 行(问题)。当应用程序启动时,我想从 JSON 加载信息。在 title 中,我想从 question.number 中加载信息,在 textLabel.text 中,我想从 question.text< 中加载信息,在 showAnswerLabel.text 中,我想从 question.answer 加载信息。当我在 textField 中写入 question.answer 的正确答案并按下键盘上的完成按钮时,我想看到 print("right")在控制台和我的设备中,我想查看下一个问题。但我有一个问题。当应用程序启动时,我从 JSON 加载第一行 ({"number": "1", "text": "1", "answer": "1"},)。在我的 showAnswerLabel.text answer = 1 中。但是当我在 textField 中写入 1 并按下完成按钮时,我在控制台中没有得到 print("right") 。但是如果我从第二行写 answer = 2 ({"number": "2", "text": "2", "answer": "2"},) 从 textField 中的 JSON 并按完成按钮我在控制台中得到 print("right")。但正确答案应该是 showAnswerLabel.text 中的数字,即 1。如何解决?

我的新代码:

{
"questions" : [{"number": "1", "text": "1", "answer": "1"},
{"number": "2", "text": "2", "answer": "2"},
{"number": "3", "text": "3", "answer": "3"}]
}

struct Root : Decodable {
let questions : [Question]
}

struct Question : Decodable {
let number, text, answer : String
}

class ViewController: UIViewController, UITextFieldDelegate {

var counter = 0
var questions = [Question]()
@IBOutlet var textLabel: UILabel!
@IBOutlet var showAnswerLabel: UILabel!
@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

let url = Bundle.main.url(forResource: "0", withExtension: "json")!
let data = try! Data(contentsOf: url)
let result = try! JSONDecoder().decode(Root.self, from: data)
self.questions = result.questions

textField.delegate = self
textField.returnKeyType = .done
_ = textFieldShouldReturn(textField)

}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()

let question = questions[counter]
title = question.number
textLabel.text = question.text
showAnswerLabel.text = question.answer
counter = (counter + 1) % questions.count

if textField.text == question.answer {
print("right")

}

return true
}

最佳答案

textFieldShouldReturn 中提取 UI 更新并绑定(bind)到 counter 的更改的可能修复:

class ViewController: UIViewController, UITextFieldDelegate 
{
var questions = [Question]()
@IBOutlet var textLabel: UILabel!
@IBOutlet var showAnswerLabel: UILabel!
@IBOutlet weak var textField: UITextField!

var counter: Int = -1 {
didSet {
guard counter >= 0, counter < questions.count else { fatalError() }
let question = questions[counter]
updateUI(with: question)
}
}

override func viewDidLoad()
{
super.viewDidLoad()

let url = Bundle.main.url(forResource: "0", withExtension: "json")!
let data = try! Data(contentsOf: url)
let result = try! JSONDecoder().decode(Root.self, from: data)
self.questions = result.questions
textField.delegate = self
textField.returnKeyType = .done

counter = 0 // will trigger `updateUI(with: questions[0])`
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()

let question = questions[counter]
if textField.text == question.answer {
print("right")
// increment counter only answer is right
// will trigger an UI update
counter = (counter + 1) % questions.count
}

return true
}

func updateUI(with question: Question) {
title = question.number
textLabel.text = question.text
showAnswerLabel.text = question.answer
}
}

我不会完全按照这个来做,但你明白了。

关于ios - 从 JSON 加载信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48183879/

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