为简单起见,我有两个课程Location
和Program
。 Location
有objectId
和name
,而Program
有objectId
,name
和location
(这是Location
的指针1}} 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.......
答案 0 :(得分:2)
所以我找到了答案。
当我将位置传递给程序视图控制器时,我需要将其作为PFObject传递。
var passedLocationID: PFObject?
然后只是一个简单的查询。
let newQuery = PFQuery(className: "Program")
newQuery.whereKey("location", equalTo: passedLocationID!)
答案 1 :(得分:0)
添加针对 Swift 3 + 更新的正确答案,以防有人搜索:
guard let query = ProgramModel.query(), let pointerQuery = LocationModel.query() else {
// Your parse class isn't set up correctly. Handle error.
return
}
// You can also use other parameters like `notEqualTo` or `containedIn`.
pointerQuery.whereKey("objectId", equalTo: passedLocationId)
// The important line is the one below, which calls the pointer query:
query.whereKey("location", matchesQuery: pointerQuery)
query.includeKey("location")
query.findObjects... or query.countObjects...