如何在Swift中随机化JSON中的数据?

huangapple go评论64阅读模式
英文:

How to randomize data inside a JSON in Swift?

问题

我的目标是编写一个情侣应用程序,让他们可以互相提问。我遇到了问题,因为我似乎无法随机化数据。

我希望每次点击按钮时数据都能随机化。

以下是我的JSON数据:

[
    {
        "questionNumber": 0,
        "question": "你们是否有未来共同实现的梦想?"
    },
    {
        "questionNumber": 1,
        "question": "作为一对夫妻,你们如何处理压力和困难?"
    },
    {
        "questionNumber": 2,
        "question": "你们最喜欢的共同旅行记忆是什么?"
    }
]

以下是我的JSONManager

import Foundation

struct QuestionJSON: Codable {
    
    var questionNumber: Int
    var question: String
    
}

extension Bundle {
    func decode<T: Decodable>(_ type: T.Type, from file: String, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) -> T {
        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError("无法在捆绑包中找到 \(file)。")
        }

        guard let data = try? Data(contentsOf: url) else {
            fatalError("无法从捆绑包中加载 \(file)。")
        }

        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = dateDecodingStrategy
        decoder.keyDecodingStrategy = keyDecodingStrategy

        do {
            return try decoder.decode(T.self, from: data)
        } catch DecodingError.keyNotFound(let key, let context) {
            fatalError("由于找不到缺少的键 '\(key.stringValue)' 而无法从捆绑包中解码 \(file) - \(context.debugDescription)")
        } catch DecodingError.typeMismatch(_, let context) {
            fatalError("由于类型不匹配,无法从捆绑包中解码 \(file) - \(context.debugDescription)")
        } catch DecodingError.valueNotFound(let type, let context) {
            fatalError("由于缺少 \(type) 值,无法从捆绑包中解码 \(file) - \(context.debugDescription)")
        } catch DecodingError.dataCorrupted(_) {
            fatalError("由于似乎是无效的 JSON,无法从捆绑包中解码 \(file)")
        } catch {
            fatalError("从捆绑包中解码 \(file) 时发生错误:\(error.localizedDescription)")
        }
    }
}

以下是出现错误的代码:

struct TestView: View {
    @State private var question: QuestionJSON?
    
    var body: some View {

        VStack {
            if let question = question {
                VStack {
                    Text(String(question.questionNumber))
                        .font(.system(.title3))
                        .foregroundColor(.white)

                }.frame(width: 240)
                .background(RoundedRectangle(cornerRadius: 7.0).fill(Color.blue))
            }
        }.onAppear {
            let questions = Bundle.main.decode(QuestionJSON.self, from: "data.json")
            question = questions.randomElement()
        }
       
    }
}

这是我无法随机化问题时的错误:

如何在Swift中随机化JSON中的数据?

正如我所说,我的目标是创建一个按钮,每次点击它时问题都会更改。

英文:

My goal is to code an app for couples so they can ask questions to each other. I'm stuck because I can't seem to randomize the data.

I want the data to be randomize every time I click the button.

Here is my JSON data:

[
    {
        &quot;questionNumber&quot;: 0,
        &quot;question&quot;: &quot;Do you have any dreams of achieving something together in the future?&quot;
    },
    {
        &quot;questionNumber&quot;: 1,
        &quot;question&quot;: &quot;How do you handle stress and difficulties as a couple?&quot;
    },
    {
        &quot;questionNumber&quot;: 2,
        &quot;question&quot;: &quot;What is your favorite shared travel memory?&quot;
    }
]

Here is my JSONManager:

import Foundation


struct QuestionJSON: Codable {
    
    var questionNumber: Int
    var question: String
    
}

extension Bundle {
    func decode&lt;T: Decodable&gt;(_ type: T.Type, from file: String, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) -&gt; T {
        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError(&quot;Failed to locate \(file) in bundle.&quot;)
        }

        guard let data = try? Data(contentsOf: url) else {
            fatalError(&quot;Failed to load \(file) from bundle.&quot;)
        }

        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = dateDecodingStrategy
        decoder.keyDecodingStrategy = keyDecodingStrategy

        do {
            return try decoder.decode(T.self, from: data)
        } catch DecodingError.keyNotFound(let key, let context) {
            fatalError(&quot;Failed to decode \(file) from bundle due to missing key &#39;\(key.stringValue)&#39; not found  \(context.debugDescription)&quot;)
        } catch DecodingError.typeMismatch(_, let context) {
            fatalError(&quot;Failed to decode \(file) from bundle due to type mismatch  \(context.debugDescription)&quot;)
        } catch DecodingError.valueNotFound(let type, let context) {
            fatalError(&quot;Failed to decode \(file) from bundle due to missing \(type) value  \(context.debugDescription)&quot;)
        } catch DecodingError.dataCorrupted(_) {
            fatalError(&quot;Failed to decode \(file) from bundle because it appears to be invalid JSON&quot;)
        } catch {
            fatalError(&quot;Failed to decode \(file) from bundle: \(error.localizedDescription)&quot;)
        }
    }
}

Here is the code where the error shows up:

struct test : View {
    @State private var question : QuestionJSON?
    
    var body: some View {

        VStack {
            if let question = question {
                VStack {
                    Text(String(question.questionNumber))
                        .font(.system(.title3))
                        .foregroundColor(.white)

                }.frame(width: 240)
                .background(RoundedRectangle(cornerRadius: 7.0).fill(Color.blue))
            }
        }.onAppear {
            let questions = Bundle.main.decode(QuestionJSON.self, from: &quot;data.json&quot;)
            question = questions.randomElement()
        }
        
       
    }
}

Here is error when I can't randomize my questions:

如何在Swift中随机化JSON中的数据?

As I said, my goal is to create the button and every time I click it, the question changes.

答案1

得分: 1

这一行有错误:

let questions = Bundle.main.decode(QuestionJSON.self, from: &quot;data.json&quot;)

这会解码一个 QuestionJSON。你有一个 QuestionJSON 的数组,所以你的意思是:

let questions = Bundle.main.decode([QuestionJSON].self, from: &quot;data.json&quot;)
                                   ^^^^^^^^^^^^^^
英文:

This line is incorrect:

let questions = Bundle.main.decode(QuestionJSON.self, from: &quot;data.json&quot;)

This decodes exactly one QuestionJSON. You have an array of them, so you meant:

let questions = Bundle.main.decode([QuestionJSON].self, from: &quot;data.json&quot;)
                                   ^^^^^^^^^^^^^^

huangapple
  • 本文由 发表于 2023年6月22日 03:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定