Error with Parse Query findObjectsInBackgroundWithBlock

依然范特西╮ 提交于 2020-01-11 05:41:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32580364/error-with-parse-query-findobjectsinbackgroundwithblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!