为简单起见,我有两个类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 {
}
该查询将以下错误打印到控制台“指针字段位置需要指针值”
返回关联程序的正确代码是什么?
我已经找到了解决方案,但似乎有些矫kill过正。两个查询进行解析,我觉得应该只使用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.......
所以我找到了答案。
将位置传递给程序视图控制器时,需要将其作为PFObject传递。
var passedLocationID: PFObject?
然后只需一个简单的查询即可。
let newQuery = PFQuery(className: "Program")
newQuery.whereKey("location", equalTo: passedLocationID!)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句