菜鸟教程小白 发表于 2022-12-11 19:34:58

ios - 从 JSON 加载信息


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

<p>我的新代码:</p>

<pre><code>{
    &#34;questions&#34; : [{&#34;number&#34;: &#34;1&#34;, &#34;text&#34;: &#34;1&#34;, &#34;answer&#34;: &#34;1&#34;},
                   {&#34;number&#34;: &#34;2&#34;, &#34;text&#34;: &#34;2&#34;, &#34;answer&#34;: &#34;2&#34;},
                   {&#34;number&#34;: &#34;3&#34;, &#34;text&#34;: &#34;3&#34;, &#34;answer&#34;: &#34;3&#34;}]
}

struct Root : Decodable {
    let questions :
}

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

class ViewController: UIViewController, UITextFieldDelegate {

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

override func viewDidLoad() {
      super.viewDidLoad()

      let url = Bundle.main.url(forResource: &#34;0&#34;, withExtension: &#34;json&#34;)!
      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) -&gt; Bool {
      textField.resignFirstResponder()

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

      if textField.text == question.answer {
            print(&#34;right&#34;)

      }

      return true
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>从 <code>textFieldShouldReturn</code> 中提取 UI 更新并绑定(bind)到 <code>counter</code> 的更改的可能修复:</p>

<pre><code>class ViewController: UIViewController, UITextFieldDelegate
{
    var questions = ()
    @IBOutlet var textLabel: UILabel!
    @IBOutlet var showAnswerLabel: UILabel!
    @IBOutlet weak var textField: UITextField!

    var counter: Int = -1 {
       didSet {
         guard counter &gt;= 0, counter &lt; questions.count else { fatalError() }      
         let question = questions
         updateUI(with: question)
       }
    }

    override func viewDidLoad()
    {
      super.viewDidLoad()

      let url = Bundle.main.url(forResource: &#34;0&#34;, withExtension: &#34;json&#34;)!
      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)`
    }

    func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
      textField.resignFirstResponder()

      let question = questions      
      if textField.text == question.answer {
            print(&#34;right&#34;)
            // 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
    }
}
</code></pre>

<p>我不会完全这样做,但你明白了。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从 JSON 加载信息,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48183879/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48183879/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从 JSON 加载信息