【问题标题】:Fetching data from parse to Table View Controller从解析获取数据到表视图控制器
【发布时间】:2015-10-16 22:05:34
【问题描述】:

我正在尝试从解析中获取数据并将其呈现在表格视图中。由于出现错误,我无法编译代码。

这是我的代码:

import UIKit
import Parse

class EventsTableViewController: UITableViewController {

    // Array to store event timeline objects from parse
    var eventtimelineData:NSMutableArray = NSMutableArray()


    override func viewDidAppear(animated: Bool) {
        self.loadData()
    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }


    @IBAction func loadData(){

        // first remove the contents from the array
        eventtimelineData.removeAllObjects()

        var findTimelineData:PFQuery = PFQuery(className: "Events")

        findTimelineData.findObjectsInBackgroundWithBlock{
             (objects: [AnyObject]!, error: NSError!) -> Void in

            // if query doesn't return any error
            if !error{
                for object:PFObject! in objects{
                    self.timelineData.addObject(object)
                }

                let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects
                self.timelineData = array as NSMutableArray

                self.tableView.reloadData()


            }

        }
    }

这是得到的错误:

([AnyObject]!, NSError!) -> Void' 不能转换为 'PFArrayResultBlock?

你能帮我解决这个问题吗?谢谢。

【问题讨论】:

  • 我相信 NSError!应该是 NSError?如果你愿意,你可以只写 objects,error 而不是 objects : [AnyObject]!,error : NSError!
  • 这样尝试(结果:[AnyObject],错误:NSError)-> Void in
  • 谢谢乔希,它成功了。

标签: ios uitableview parse-platform


【解决方案1】:

想通了。 Parse 更新了他们的查询语法。这行得通。

@IBAction func loadData(){

        // first remove the contents from the array
        eventtimelineData.removeAllObjects()


        let query = PFQuery(className:"Events")
        query.whereKey("CreatedBy", equalTo:PFUser.currentUser()!)

        query.findObjectsInBackgroundWithBlock {
            (objects, error) -> Void in

            if error == nil {
                // The find succeeded.
                print("Successfully retrieved \(objects!.count) scores.")

                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {

                        self.eventtimelineData.addObject(object)
                        print(object.objectId)
                        print(object.description)
                    }

                    let array:NSArray = self.eventtimelineData.reverseObjectEnumerator().allObjects
                    self.eventtimelineData = array.mutableCopy() as! NSMutableArray
                    self.tableView.reloadData()

                }


            } else {
                // Log details of the failure
                print("Done")
                print("Error: \(error!) \(error!.userInfo)")
            }
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2022-10-13
    • 2014-06-12
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多