使用swift进行iOS编码,我正在使用解析。我需要进行查询以使用其ID检索1个对象。 问题是当我查询找到几个对象时查询是成功的,但是当我查询找到1个对象时却不成功:
此代码有效:
var queryEvent = PFQuery(className: "Event")
queryEvent.whereKey("objectId", equalTo: objectId)
queryEvent.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
println("Successfully retrieved the object.")
} else {
// Log details of the failure
println("error: \(error)")
}
}
此代码不起作用:
var queryEvent = PFQuery(className: "Event")
queryEvent.getObjectInBackgroundWithId(objectId, block: { (object: PFObject!, error: NSError!) -> Void in
if object != nil {
println("The getFirstObject request failed.")
println(error) //contain nil
} else {
// The find succeeded.
println("Successfully retrieved the object.")
}
})
所以我想使用方法getObjectInBackgroundWithId,因为它更合适,但我不知道它失败的原因。感谢
编辑:以下是Parse I的代码示例。
var query = PFQuery(className:"GameScore")
query.whereKey("playerEmail", equalTo:"dstemkoski@example.com")
query.getFirstObjectInBackgroundWithBlock {
(object: PFObject!, error: NSError!) -> Void in
if object != nil {
NSLog("The getFirstObject request failed.")
} else {
// The find succeeded.
NSLog("Successfully retrieved the object.")
}
}
解决方案在以下评论中。谢谢