我希望从特定商店获取名为Sticker的Parse.com表格中的所有商品。我的Sticker表有一个名为shopId的列。所以显而易见的解决方案就是:
//get all stickers from one shop of category dress
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: "QjSbyC6k5C")
query.whereKey("category", equalTo: "DR")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
然而,这会导致此错误:
错误:指针字段shopId需要指针值
我看到一个常见的解决方案似乎是将查询传递给实际对象而不是ID的字符串。这是否意味着我必须首先执行单独的查询以获取特定的商店对象,然后将其传递给我的查询?或者是否有更短的方式?
这是我试图去商店但是它导致了这个错误:
只能在符合的子类上调用 - [PFObject init] PFSubclassing
var query1 = PFQuery(className: "Shop")
var shop1 = PFObject()
query1.getObjectInBackgroundWithId("QjSbyC6k5C") {
(shop: PFObject?, error: NSError?) -> Void in
shop1 = shop!
}
编辑:所以我的解决方案基本上是按照建议做的。我的代码是这个(魅力是商店的名称):
var shopQuery = PFQuery(className:"Shop")
shopQuery.getObjectInBackgroundWithId("QjSbyC6k5C") {
(glamour: PFObject?, error: NSError?) -> Void in
if error == nil && glamour != nil {
println(glamour)
//get all stickers from one shop of category dress
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: glamour!)
query.whereKey("category", equalTo: "DR")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
} else {
println(error)
}
}
我会在这里留下这个问题,或许有人会回答一下评论:有没有办法让商店和它的类范围,以便我们不必将第二个查询嵌套在成功的第一次查询?那会更优雅吗?
答案 0 :(得分:1)
你需要传递PFObject。使用以下
更改您的代码PFObject *object = ...
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: "QjSbyC6k5C")
query.whereKey("category", equalTo: object);