英文:
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()
        }
       
    }
}
这是我无法随机化问题时的错误:

正如我所说,我的目标是创建一个按钮,每次点击它时问题都会更改。
英文:
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:
[
    {
        "questionNumber": 0,
        "question": "Do you have any dreams of achieving something together in the future?"
    },
    {
        "questionNumber": 1,
        "question": "How do you handle stress and difficulties as a couple?"
    },
    {
        "questionNumber": 2,
        "question": "What is your favorite shared travel memory?"
    }
]
Here is my 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("Failed to locate \(file) in bundle.")
        }
        guard let data = try? Data(contentsOf: url) else {
            fatalError("Failed to load \(file) from bundle.")
        }
        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("Failed to decode \(file) from bundle due to missing key '\(key.stringValue)' not found – \(context.debugDescription)")
        } catch DecodingError.typeMismatch(_, let context) {
            fatalError("Failed to decode \(file) from bundle due to type mismatch – \(context.debugDescription)")
        } catch DecodingError.valueNotFound(let type, let context) {
            fatalError("Failed to decode \(file) from bundle due to missing \(type) value – \(context.debugDescription)")
        } catch DecodingError.dataCorrupted(_) {
            fatalError("Failed to decode \(file) from bundle because it appears to be invalid JSON")
        } catch {
            fatalError("Failed to decode \(file) from bundle: \(error.localizedDescription)")
        }
    }
}
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: "data.json")
            question = questions.randomElement()
        }
        
       
    }
}
Here is error when I can't randomize my questions:

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: "data.json")
这会解码一个 QuestionJSON。你有一个 QuestionJSON 的数组,所以你的意思是:
let questions = Bundle.main.decode([QuestionJSON].self, from: "data.json")
                                   ^^^^^^^^^^^^^^
英文:
This line is incorrect:
let questions = Bundle.main.decode(QuestionJSON.self, from: "data.json")
This decodes exactly one QuestionJSON. You have an array of them, so you meant:
let questions = Bundle.main.decode([QuestionJSON].self, from: "data.json")
                                   ^^^^^^^^^^^^^^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论