gpt4 book ai didi

ios - 如何计算 parse.com 类中的对象以获得从中提取随机数的总数?

转载 作者:行者123 更新时间:2023-11-28 07:08:51 25 4
gpt4 key购买 nike

当我尝试计算我的 parse.com 类的对象时出现错误。我需要计算对象的总数,这样我就可以得到一个随机数并使用它来查询一个对象。我的数据表中有一个名为 Qnum 的列,它为每个对象编号。这是我的功能:

func CallData() {

var query = PFQuery(className: "QuestionsandAnswers")
let randomNumber = arc4random_uniform(count)
var randomNumberCast = Int(randomNumber)
query.whereKey("Qnum", equalTo: randomNumberCast)
query.getFirstObjectInBackgroundWithBlock { (object: PFObject!, error: NSError!) -> Void in
if (error == nil) {
self.Question = object ["Question"] as String!
self.Answers = object ["Answers"] as Array!
self.Answer = object ["Answer"] as String!

if (self.Answers.count > 0) {
self.QuestionLabel.text = self.Question

self.Button1.setTitle(self.Answers[0], forState: UIControlState.Normal)
self.Button2.setTitle(self.Answers[1], forState: UIControlState.Normal)
self.Button3.setTitle(self.Answers[2], forState: UIControlState.Normal)
self.Button4.setTitle(self.Answers[3], forState: UIControlState.Normal)

}


} else {

NSLog("Something is wrong, dude. Sorry.")
}
}
}

我的错误出现在函数“let randomNumber = arc4random_uniform(count)”的第二行,我试过 (objects.count) 无济于事;然而,当我只插入对象总数时,它起作用了,(20) 但我想避免对该数字进行硬编码,以防我需要向我的类(class)添加更多对象。对这个新手有什么建议吗?

解决方案: danh 在下面提供了答案,但由于我只需要更改一些东西,而且我不知道如何在评论中发布修改后的代码,我正在编辑这个已回答的问题现在包括解决方案。这是有效的代码:

func CallData() {
var countQuery = PFQuery(className: "QuestionsandAnswers")
countQuery.countObjectsInBackgroundWithBlock { (count: Int32, error: NSError!) -> Void in
if (error == nil) {
let randomNumber = Int(arc4random_uniform(UInt32(count)))
var query = PFQuery(className: "QuestionsandAnswers")
query.skip = randomNumber
query.limit = 1
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if (error == nil) {
var object: AnyObject = objects[0]
self.Question = object ["Question"] as String!
self.Answers = object ["Answers"] as Array!
self.Answer = object ["Answer"] as String!

if (self.Answers.count > 0) {
self.QuestionLabel.text = self.Question
self.Button1.setTitle(self.Answers[0], forState: UIControlState.Normal)
self.Button2.setTitle(self.Answers[1], forState: UIControlState.Normal)
self.Button3.setTitle(self.Answers[2], forState: UIControlState.Normal)
self.Button4.setTitle(self.Answers[3], forState: UIControlState.Normal)
}
} else {
NSLog("Something is wrong with the find request, dude. Sorry. %@", error)
}
}
} else {
NSLog("Something is wrong with the count request, dude. Sorry. %@", error)
}
}
}

最佳答案

只有知道计数才能使用,知道需要调用计数 api。一旦你有了计数,你就可以用它来绑定(bind)一个随机数。

random 可用于基于“Qnum”字段的查找,或用作不合格查询的跳过。跳过不太容易出错,因为它仅取决于计数。

不是说得很快,我尽力拼凑出这条建议的快速形式。以下内容在语法上可能有点错误,但我很确定它的基本思想是正确的...

    var countQuery = PFQuery(className: "QuestionsandAnswers")
countQuery.countObjectsInBackgroundWithBlock { (count: Int, error: NSError!) -> Void in
if (error == nil) {
let randomNumber = Int(arc4random_uniform(UInt32(count)))
var query = PFQuery(className: "QuestionsandAnswers")
query.skip = randomNumber
query.limit = 1
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if (error == nil) {
var object: AnyObject = objects[0]
self.Question = object ["Question"] as String!
self.Answers = object ["Answers"] as Array!
self.Answer = object ["Answer"] as String!

if (self.Answers.count > 0) {
self.QuestionLabel.text = self.Question
self.Button1.setTitle(self.Answers[0], forState: UIControlState.Normal)
self.Button2.setTitle(self.Answers[1], forState: UIControlState.Normal)
self.Button3.setTitle(self.Answers[2], forState: UIControlState.Normal)
self.Button4.setTitle(self.Answers[3], forState: UIControlState.Normal)
}
} else {
NSLog("Something is wrong with the find request, dude. Sorry. %@", error)
}
}
} else {
NSLog("Something is wrong with the count request, dude. Sorry. %@", error)
}
}

关于ios - 如何计算 parse.com 类中的对象以获得从中提取随机数的总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29332523/

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