gpt4 book ai didi

ios - Parse.com 查询以获取具有特定指针的所有项目

转载 作者:行者123 更新时间:2023-11-29 01:39:59 24 4
gpt4 key购买 nike

我想从我的 Parse.com 表(名为 Sticker)中的特定商店获取所有商品。我的 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!)")
}
}

但是这会导致此错误:

error: pointer field shopId needs a pointer value

我看到一个常见的解决方案似乎是向查询传递实际对象而不是 ID 字符串。这是否意味着我必须首先执行单独的查询来获取特定的商店对象,然后将其传递给我的查询?或者有更短的方法吗?

这是我尝试获取商店的方法,但它导致了此错误:

Can only call -[PFObject init] on subclasses conforming to 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)
}
}

我会把这个问题留在这里,也许有人会用评论来回答:有没有办法获取商店并赋予它类范围,这样我们就不必将第二个查询嵌套在成功的第一个查询?这样会更优雅吗?

最佳答案

您需要传递 PFObject。使用以下代码更改您的代码

PFObject *object = ...
var query = PFQuery(className:"Sticker")
query.whereKey("shopId", equalTo: "QjSbyC6k5C")
query.whereKey("category", equalTo: object);

关于ios - Parse.com 查询以获取具有特定指针的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516133/

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