我正在尝试重新编写此代码,该代码从我的Parse数据库中检索随机数据行。这是Objective-C中的代码:
// Get the index for the new object.
PFObject *master = [[PFQuery queryWithClassName:@"Master"] getFirstObject];
[master incrementKey:@"nextCardIndex"];
[master save];
int index = [master objectForKey:@"nextCardIndex"] - 1;
// Create the object itself and assign it the id.
PFObject *card = [PFObject objectWithClassName:@"Card"];
[card setObject:[NSNumber numberWithInt:index] forKey:@"index"];
[card setObject:@"king of hearts" forKey:@"description"];
[card save];
// Get the max index of any card (+1).
PFObject *master = [[PFQuery queryWithClassName:@"Master"] getFirstObject];
int maxIndex = [master objectForKey:@"nextCardIndex"];
// Randomly pick an index in the range of all indices.
int randomIndex = arc4random() % maxIndex;
// Get the card with that particular index.
PFQuery *cardQuery = [PFQuery queryWithClassName:@"Card"];
[cardQuery whereKey:@"index" equalTo:[NSNumber numberWithInt:randomIndex]];
PFObject *card = [cardQuery getFirstObject];
到目前为止,我已将其解释为这样写:
@IBOutlet var showOption1: UILabel!
@IBOutlet var showOption2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var voteCount1 = PFObject(className: "VoteCount")
voteCount1["choices"] = 2
voteCount1["votes"] = Int()
voteCount1["votes2"] = Int()
voteCount1["optionName"] = String()
voteCount1["optionName2"] = String()
voteCount1["objectId"] = String()
var voteCount2 = PFObject(className: "VoteCount2")
voteCount2["choices"] = 3
voteCount2["votes"] = Int()
voteCount2["votes2"] = Int()
voteCount2["votes3"] = Int()
voteCount2["optionName"] = String()
voteCount2["optionName2"] = String()
voteCount2["optionName3"] = String()
var voteCount3 = PFObject(className: "VoteCount3")
voteCount3["choices"] = 4
voteCount3["votes"] = Int()
voteCount3["votes2"] = Int()
voteCount3["votes3"] = Int()
voteCount3["votes4"] = Int()
voteCount3["optionName"] = String()
voteCount3["optionName2"] = String()
voteCount3["optionName3"] = String()
voteCount3["optionName4"] = String()
var query = PFQuery(className: "VoteCount")
query.countObjectsInBackgroundWithBlock {
(count: Int32, error: NSError!) -> Void in
if error == nil {
let randNumber = Int(arc4random_uniform(UInt32(count)))
query.whereKey("voteNumber", equalTo: randNumber)
query.getFirstObjectInBackgroundWithBlock {
(voteCount1: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
let votes = voteCount1["votes"] as Int
let votes2 = voteCount1["votes2"] as Int
let option1 = voteCount1["optionName"] as String
let option2 = voteCount1["optionName2"] as String
self.showOption1.text = "\(option1)"
self.showOption2.text = "\(option2)"
}
}
} else {
println("error \(error)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var pollResults: UILabel!
@IBAction func addVote1(sender: AnyObject) {
var query = PFQuery(className: "VoteCount")
query.getObjectInBackgroundWithId("voteNumber") {
(voteCount1: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
voteCount1.incrementKey("votes")
voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
let votes = voteCount1["votes"] as Int
let votes2 = voteCount1["votes2"] as Int
self.pollResults.text = "\(votes) \(votes2)"
}
}
}
问题是我无法让IBAction在第一个var查询下的viewDidLoad函数下随机调用的objectId中的值添加投票。到目前为止我错过了什么?我已将其更改为添加一些自己的类名,即VoteCount。我把它放在了正确的地方吗?我想做的就是从我的Parse数据库中随机调用所有数据。