gpt4 book ai didi

ios - swift + 火力地堡 : randomly picking a group of objects using a query to the server

转载 作者:搜寻专家 更新时间:2023-11-01 06:40:56 24 4
gpt4 key购买 nike

我正在使用 Swift 并使用 Firebase 作为后端为 iOS 构建一个测验应用程序。我希望能够进行一个查询,随机选择 10 个问题并返回它们。

enter image description here

最佳答案

首先,对于我的回答,您需要为每个问题赋予如下值:

    {
"question1": {
"question" : "Do you know swift",
"answer" : "Nope",
"value": 1
},
"question2": {
"question" : "Do you know firebase",
"answer" : "A bit",
"value" : 2
}
}

之后,建议在您的 firebase 规则 ( firebase docs ) 中添加一个索引,如下所示:

    {
"rules": {
"questions": {
".indexOn": ["value"]
}
}
}

接下来是快速的部分:

//Use a for loop to get 10 questions
for _ in 1...10{
//generate a random number between 1 and the amount of questions you have
var randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1

//The reference to your questions in firebase (this is an example from firebase itself)
let ref = Firebase(url: "https://dinosaur-facts.firebaseio.com/dinosaurs")
//Order the questions on their value and get the one that has the random value
ref.queryOrderedByChild("value").queryEqualToValue(randomNumber)
.observeEventType(.ChildAdded, withBlock: {
snapshot in
//Do something with the question
println(snapshot.key)
})
}

实际的 swift 代码可能存在缺陷,对于 firebase 特定代码,请查看 Ios documentation

关于ios - swift + 火力地堡 : randomly picking a group of objects using a query to the server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com