我正试图从解析加载一页关于Swift对象的信息。给出的代码允许您一次检索整个类。但如何在不访问Parse上的仪表板的情况下检索ObjectID呢?
这段代码来自Parse
var query = PFQuery(className:"GameScore")
query.getObjectInBackgroundWithId("xWMyZEGZ") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error == nil && gameScore != nil {
println(gameScore)
} else {
println(error)
}
}
一切都是直截了当的,但我仍然不知道如何获得随机生成的ObjectID
如果我正确理解你的问题,你想查询所有对象并列出其id,例如:
var query = PFQuery(className:"GameScore")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
println("Successfully retrieved (objects.count) scores.")
//list of Ids
for object in objects {
objectIds.append(object.objectId as String)
}
} else {
println("(error)")
}
}
做到这一点的最好方法是在解析仪表板中有两个单独的类。根据你的例子,一个是carTypes,另一个是carDetails类,带有指向carTypes的指针。
当你点击一辆汽车时,你可以使用来自carTypes的objectId来查询carDetails类。
例如 // you have to give this object some value when you click on a button or cell..etc
var selectedCar : PFObject?
//in the query i didn't use selectedCar.objectId because i am using pointers to the carType class so parse will get the objectId for you
let query = PFQuery(className: "carDetails")
query.whereKey("carType", equalTo: selectedCar)
query.findObjectsInBackgroundWithBlock({ (object: [PFObject]?,error: NSError?) -> Void in
if error == nil {
some code here
}
})