1

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()
        }
    }
}
rici
  • 234,347
  • 28
  • 237
  • 341
jmcastel
  • 1,365
  • 7
  • 17
  • 34
  • 1
    What line is the code crashing on? Does the console give you any output when it crashes? Is self.tableView.reloadData() running on the main thread? Maybe try commenting out lines of code until it doesn't crash – Michael Wildermuth Aug 26 '14 at 22:23

2 Answers2

2

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!

0

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 :)

marciokoko
  • 4,988
  • 8
  • 51
  • 91
  • `weakSelf!` could cause a crash; it should be checked with `if weakSelf != nil` first. Also, you can add `[weak self] in` at the beginning of the block and then use `self` directly instead of creating a whole new `weakSelf` variable. – Mike S Sep 09 '14 at 00:28