Parse.com 使用 Swift 获取 ObjectID
Parse.com get ObjectIDs with Swift
我目前从我的第一个解析步骤开始,但目前我停留在一个非常基本的点上。
有什么方法可以从我的 "parseObject" 中取回包含我所有 "objectID" 列表的数组吗?
我只是想要一个数组,从一个 "table"
中获取所有自动设置的 objectID
如果我正确理解你的问题,你需要查询你的对象,例如:
PFQuery *userPhotosQuery = [PFQuery queryWithClassName:@"photos"];
[userPhotosQuery whereKey:@"user"equalTo:[PFUser currentUser]];
[userPhotosQuery orderByDescending:@"createdAt"];
这将 return 当前用户保存的所有照片对象。
您也可以添加任何其他过滤器。
如果我错了或没有正确回答问题,请纠正我。
这是一个Swift解决方案:
// objectIds is your array to store the objectId values returned from parse
// objectId is a String
var objectIds:[""] // empty string array
func loadDataFromParse () {
var query = PFQuery(className:"Record")
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
for object in objects {
objectIds.append(object.objectId as String)
}
} else {
println("\(error)")
}
}
}
// this function will retrieve a photo in the record with specified objectId
// and store it in noteImage
var noteImage = UIImage() // where retrieved image is stored
func loadImageFromParse (objectId: String) {
var query = PFQuery(className:"Record")
query.whereKey("objectId", equalTo:objectId)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
println("Successfully retrieved \(objects.count) records.")
for object in objects {
let userImageFile = object["image"] as PFFile!
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
noteImage = UIImage(data:imageData)!
println("Image successfully retrieved")
}
}
}
} else {
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
}
我认为 Parse 没有提供获取数组中所有 objectId 的方法。或者,您可以遍历每个对象并将 objectId 检索为:
var objectIds = [String]()
let query = PFQuery(className:"TableName")
query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
objectIds.append(String(object.valueForKey("objectId")!))
}
}
} else {
print("Error: \(error!) \(error!.userInfo)")
}
print(objectIds)
}
我目前从我的第一个解析步骤开始,但目前我停留在一个非常基本的点上。 有什么方法可以从我的 "parseObject" 中取回包含我所有 "objectID" 列表的数组吗?
我只是想要一个数组,从一个 "table"
中获取所有自动设置的 objectID如果我正确理解你的问题,你需要查询你的对象,例如:
PFQuery *userPhotosQuery = [PFQuery queryWithClassName:@"photos"];
[userPhotosQuery whereKey:@"user"equalTo:[PFUser currentUser]];
[userPhotosQuery orderByDescending:@"createdAt"];
这将 return 当前用户保存的所有照片对象。 您也可以添加任何其他过滤器。 如果我错了或没有正确回答问题,请纠正我。
这是一个Swift解决方案:
// objectIds is your array to store the objectId values returned from parse
// objectId is a String
var objectIds:[""] // empty string array
func loadDataFromParse () {
var query = PFQuery(className:"Record")
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
for object in objects {
objectIds.append(object.objectId as String)
}
} else {
println("\(error)")
}
}
}
// this function will retrieve a photo in the record with specified objectId
// and store it in noteImage
var noteImage = UIImage() // where retrieved image is stored
func loadImageFromParse (objectId: String) {
var query = PFQuery(className:"Record")
query.whereKey("objectId", equalTo:objectId)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
println("Successfully retrieved \(objects.count) records.")
for object in objects {
let userImageFile = object["image"] as PFFile!
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
noteImage = UIImage(data:imageData)!
println("Image successfully retrieved")
}
}
}
} else {
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
}
我认为 Parse 没有提供获取数组中所有 objectId 的方法。或者,您可以遍历每个对象并将 objectId 检索为:
var objectIds = [String]()
let query = PFQuery(className:"TableName")
query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
objectIds.append(String(object.valueForKey("objectId")!))
}
}
} else {
print("Error: \(error!) \(error!.userInfo)")
}
print(objectIds)
}