【发布时间】:2014-11-26 22:35:34
【问题描述】:
我不确定这是否与 didReceiveMemoryWarning 下面的 IBAction 没有调用与 viewDidLoad 方法下面的查询相同的变量有关,还是其他问题。但是,当我运行此代码时,我收到一条错误消息,提示“没有与查询匹配的结果”和“操作无法完成。解析错误 101”。谁能告诉我这是为什么?
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 = arc4random_uniform(UInt32(count))
query.whereKey("voteNumber", equalTo: NSNumber(unsignedInt: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("objectId") {
(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)"
}
}
}
@IBOutlet weak var pollResults2: UILabel!
@IBAction func addVote2(sender: AnyObject) {
var query = PFQuery(className: "VoteCount")
query.getObjectInBackgroundWithId("objectId") {
(voteCount1: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
voteCount1.incrementKey("votes2")
voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
let votes = voteCount1["votes"] as Int
let votes2 = voteCount1["votes2"] as Int
self.pollResults2.text = "\(votes) \(votes2)"
}
}
}
}
我要做的是从我的 Parse 数据库的随机行中调用变量“votes”、“votes2”、“option1”和“option2”的值。我创建了一个名为“voteNumber”的 Int 数据列,每个包含一个 1 到 1000 的数字,我相信通过使用随机数调用一行,我应该能够从随机行调用所有适当的数据。我只是不确定我是否将 viewDidLoad 方法中的查询与在 didReceiveMemoryWarning 方法关闭下方的 IBAction 桥接。
【问题讨论】:
标签: ios swift parse-platform