jmcastel
jmcastel

Reputation: 1365

ios swift, passing Parse PFbject in a NSArray

I tried to make a function to load data from Parse with SWIFT. Data is in the "crcl" className in Parse. I can't pass PFObject as AnyObject in my NSMutableArray "timeLineData". I have no code error but the app crash at launching.

What should i do, this is my code :

class TimelineTableViewController: UITableViewController {


var timeLineData:NSMutableArray = NSMutableArray ()


func loadData (){

    timeLineData.removeAllObjects()

    var findTimeLineData:PFQuery = PFQuery(className: "crcl")


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

            for object:AnyObject in objects!   {

                self.timeLineData.addObject(object as PFObject)
            }


            self.tableView.reloadData()
        }
    }
}

Upvotes: 1

Views: 1080

Answers (2)

Nicolaas Wagenaar
Nicolaas Wagenaar

Reputation: 170

Try to declare your timeLineData array like so:

var timeLineData = [PFObject]()

This tells Swift that timeLineData is an array containing PFObjects.

Then in your completion block use .append instead of .addObject like so:

self.timeLineData.append(object as PFObject)

That should do the trick!

Upvotes: 2

marciokoko
marciokoko

Reputation: 4986

You are calling tableView.reloadData from within the block. You need to call dispatch async like so:

dispatch_async(dispatch_get_main_queue()) {
                    weakSelf!.tableView.reloadData()
                }

where weakSelf! is a weak reference to self declared sorta like:

weak var weakSelf : MasterViewController?

where MasterViewController is whatever controller you are in :)

Upvotes: 0

Related Questions