简体   繁体   中英

Count strings inside array

This is my parse.com "currentUploads" class: Image here

How can I count the amount of strings inside the array? As you can see on the first row, there is 1, and on the last one, it is 3. How can I return this count in a println ? I have tried this:

var query = PFQuery(className:"currentUploads")
            query.whereKey("objectId", equalTo: post.objID)
            query.countObjectsInBackgroundWithBlock {
                (count: Int32, error: NSError?) -> Void in
                if error == nil {
                    print("The number is \(count) in total")
                }
            }

I know there is missing something using the "likedBy", but I dont know where to do what. Any ideas please?

The best way to count all the entries in an array column across all instances (rows) is to iterate the instances in cloud code and count each. This is done simply using the each query and summing up the length of each array then returning the result.

A similar thing can be done on the client, but you'll need to deal with pagination yourself as PFQuery doesn't offer each . In this case you would iterate the objects in each query response and sum the count of each array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM