How to count rows in a Parse class programmatically in an iOS app using swift?

后端 未结 2 1984
一整个雨季
一整个雨季 2020-12-07 03:52

I have a parse back end for my iOS app. I have a parse class called \"quiz\". How can I read the number of rows in the Parse class, I have to use it in my iOS app?

2条回答
  •  没有蜡笔的小新
    2020-12-07 04:01

    Since the count is retrieved asynchronously, the way to return the count is inside a completion closure instead of using return cnt.

    Here is an example:

    func countObjectsWithCompletion(completion: (Int?)->(Void))
    {
        var query = PFQuery(className:"quiz")
        query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in
            if error == nil
            {
                // The find succeeded.
                print("Successfully retrieved \(objects!.count) scores.")
                completion(objects!.count)
            } else {
                // Log details of the failure
                print("Error: \(error!) \(error!.userInfo)")
                completion(nil)
            }
        }
    }
    
    countObjectsWithCompletion { (myObjectCount: Int?) in
        if let cnt = myObjectCount {
            print("count is \(cnt)")
        } else {
            print("error getting count")
        }
    }
    

提交回复
热议问题