我的 JSON 文件中有 3 行(问题)。当应用程序启动时,我想从 JSON 加载信息。在 title
我想从 question.number
加载信息,在 textLabel.text
我想从 question.text< 加载信息
,在 showAnswerLabel.text
我想从 question.answer
加载信息。当我从 question.answer
中写入 textField
正确答案并按下键盘上的完成按钮时,我想看到 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/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |