Parse.com使用Swift获取ObjectID

时间:2015-02-06 12:04:13

标签: ios swift parse-platform

我目前从我的第一个解析步骤开始,但目前我坚持一个非常基本的观点。 有没有办法从我的“parseObject”中获取一个包含所有“objectID”列表的数组?

我只想要一个从一个“table”

获取所有自动设置objectID的数组

3 个答案:

答案 0 :(得分:3)

这是一个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!)
            }
        }
    }

答案 1 :(得分:1)

我不认为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)
    } 

答案 2 :(得分:0)

如果我正确理解您的问题,您需要查询对象,例如:

PFQuery *userPhotosQuery = [PFQuery queryWithClassName:@"photos"];
[userPhotosQuery whereKey:@"user"equalTo:[PFUser currentUser]];
[userPhotosQuery orderByDescending:@"createdAt"];

这将返回当前用户保存的所有照片对象。 您也可以添加任何其他过滤器。 请纠正我如果我错了或者没有正确地理解问题。