为了简单起见,我有两个类 Location
和 Program
。 Location
具有 objectId
和 name
,而 Program
具有 objectId
、name
和 location
(这是指向 Location
ObjectId
的指针)。当用户选择一个位置时,如何在查询中返回关联的节目?以下代码目前无法运行。
let pointer = PFObject(withoutDataWithClassName: "location", objectId: passedLocationID!) //passedLocationID is a string containing the ObjectId from the selected location
let query = PFQuery(className:"Program")
query.whereKey("location", equalTo: pointer.objectId!)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
self.programName.append(object["name"] as! String)
self.programID.append(object.objectId! as String)
}
}
} else {
}
此查询将以下错误打印到控制台“指针字段位置需要指针值”
返回关联程序的正确代码是什么?
编辑
我已经找到了解决方案,但似乎有点矫枉过正。对 Parse 进行两次查询,我觉得这应该仅使用 1 个查询即可完成。
let firstQuery = PFQuery(className: "Location")
firstQuery.whereKey("objectId", equalTo: passedLocationID!)
let secondQuery = PFQuery(className: "Program")
secondQuery.includeKey("location")
secondQuery.whereKey("location", matchesQuery: firstQuery)
secondQuery.findObjects.......
最佳答案
所以我找到了答案。
当我将位置传递给程序 View Controller 时,我需要将其作为 PFObject 传递。
var passedLocationID: PFObject?
然后只需一个简单的查询就可以了。
let newQuery = PFQuery(className: "Program")
newQuery.whereKey("location", equalTo: passedLocationID!)
关于ios - 使用指针解析查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562468/