ios - 在 Swift 中限制没有 query.limit 的解析查询

标签 ios swift parse-platform

我需要在同一个解析查询中做两件事。 1)我需要找到给定查询返回的对象总数; 2) 仅显示前 20 个对象。我无法通过设置 query.limit = 20 来同时执行这两项操作,因为对象总数仅为 20。如果对象总数为 100,我需要获取该数字。

那么,如何才能以编程方式仅显示前 20 个对象,同时仍接收全部 100 个对象?

var query = PFQuery(className: "Professions")
            query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
            query.orderByDescending("createdAt")
            // query.limit = 20
            query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
                if error == nil {
                    if let objects = objects as? [PFObject] {

                        for object in objects {

                        // I tried using something like:
                        // for var i = 0; i <= 20; i++ {
                        // if object[i] {
                        // But get 'Int' is not convertible to 'String'

                            if let title = object["title"] as? String {
                                println(title)
                            }

                        }
                    }

                } else {

                    println(error)

                }
            })

当我尝试设置以下内容时,我总是收到 fatal error :数组索引超出范围。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 20

    }

最佳答案

也许不是最优雅的解决方案,但我认为您需要对同一个查询执行两个查询。一种用于 object.count,另一种用于 query.limit

var query = PFQuery(className: "Professions")
        query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
            if error == nil {
                if let objects = objects as? [PFObject] {
                          var numberOfObjects = objecs.count
                    }
              else {
                println(error)
            }
        query.limit = 20
        query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
          if let objects = objects as? [PFObject] {
                    for object in objects {
                        if let title = object["title"] as? String {
                            println(title)
              }
            }
         }
         else {
                println(error)
            }
        }
        })

关于ios - 在 Swift 中限制没有 query.limit 的解析查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30922795/

相关文章:

Javascript -Uncaught promise 被拒绝,即使它已经被拒绝

ios - 如何在 Swift 中将计步器重置为 0?我正在使用 CMPedometer()

ios - 为什么 NSURLConnection 在 Swift iOS8 中失败并出现 Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."?

Swift xcode 8 - 使用多个按钮和音频文件

ios - 如何使用SpriteKit放置比场景更宽的背景图像?

ios - 解析消息传递应用程序,需要找到一种方法来延迟代码执行,直到 deleteAllInBackground 执行完毕。包含详细代码

swift - 解析正在调用的云代码无效函数

iOS MKAnnotationView LongPressGestureRecognizer

objective-c - 如何获得设备的比例因子?

ios - 在 Swift 2.0 中,我如何使标题单元格与 tableview 单元格一起移动