从JSON加载信息 [英] Loading info from JSON

查看:113
本文介绍了从JSON加载信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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中,答案= 1.但是,当我在textField中编写1并按完成"按钮时,在控制台中却没有显示print("right").但是,如果我从textField中的JSON的第二行({"number": "2", "text": "2", "answer": "2"},)中写出答案= 2,然后按完成按钮,我就会在控制台中得到print("right").但是正确的答案应该是showAnswerLabel.text中的数字,即1.如何解决?

I have a 3 line(questions) in my JSON file. When app start I want to load info from JSON. In title I want to load info from question.number, in textLabel.text I want to load info from question.text, in showAnswerLabel.text I want to load info from question.answer. And when I write in textField right answer from question.answer and press on done button on keyboard I want to see print("right") in console and in my device I want to see the next question. But I have a problem. When app start I load firs line ({"number": "1", "text": "1", "answer": "1"},) from JSON. And in my showAnswerLabel.text answer = 1. But when I write 1 in textField and press done button I not get print("right") in console. But If I write answer = 2 from second line ({"number": "2", "text": "2", "answer": "2"},) from JSON in textField and press done button I get print("right") in console. But right answer should be number from showAnswerLabel.text that is 1. How to fix it?

我的新代码:

{
    "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更新并绑定到counter的更改:

A possible fix where the UI update is extracted from textFieldShouldReturn and binded to the change of 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
    }
}

我不会那样做,但是你明白了.

I won't do it exactly like this but you get the idea.

这篇关于从JSON加载信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆