Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Parse Query findObjectsInBackgroundWithBlock

I am new to ios development and I recently found a tutorial to build a tweeter like ios app using a parse backend. My current set up is Xcode 7.1 with swift 2.0, the tutorial was done on an older version of swift so I had to adjust some of the swift syntax in order to make it work. I was doing fine until I hit the following error,

func loadData(){
    timelineData.removeAllObjects()
    var findTimelineData:PFQuery = PFQuery(className: "Tweet")

    findTimelineData.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]?, error:NSError?) -> Void in
        if (error == nil && objects != nil){
            for object:PFObject! in objects!{
               self.timelineData.addObject(object)
            }
            let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects
            self.timelineData = array as! NSMutableArray
            self.tableView.reloadData()
        }

  }

Here I am trying to access/store all the data in a parse table/class into an array. And the editor is complaining about the closure argument (objects:[AnyObject]?, error:NSError?) -> Void in. After a couple of tries,

  1. (objects:[AnyObject]!, error:NSError!) -> Void in
  2. (objects:[AnyObject], error:NSError?) -> Void in
  3. (objects:[AnyObject]?, error:NSError) -> Void in
  4. (objects:[AnyObject], error:NSError) -> Void in

All the options I tried gave me the same error: '([AnyObject]!, NSError!) -> Void' is not convertible to 'PFQueryArrayResultBlock?'

In fact for (objects:[AnyObject]?, error:NSError?) -> Void in (which I thought makes the most sense), the editor would crash and if I ran the code I would get a seg fault.

Has anyone run into similar issue? or know of a fix?

Thank you for your help in advance.

like image 265
Sam Hu Avatar asked Dec 11 '22 21:12

Sam Hu


1 Answers

Try changing [AnyObject]? to [PFObject]?. This seems to be required by Swift 2.0.

So instead of:

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

Use:

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

You'll also need to change your iteration over the array objects, since they will now already be PFObject.

like image 67
Greenwell Avatar answered Dec 26 '22 08:12

Greenwell