0

I can't seem to unwrap this. My objects variable seems to be coming back nil.

//Creating a query
let query = PFUser.query()

query?.findObjectsInBackgroundWithBlock({ (objects : [PFObject]?, error : NSError?) -> Void in
     self.users.removeAll(keepCapacity : true)       
     for object in objects!
     {
         let user : PFUser = (object as? PFUser)!
         self.users.append(user.username!)
     }
     self.tableView.reloadData()
})

enter image description here

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Divine Davis
  • 532
  • 1
  • 6
  • 15

1 Answers1

1

The issue is with the objects : [PFObject]? argument, it is an optional; that means it can be nil. In your code you are trying to forcefully unwrap it for object in objects!. Due to some error you are getting nil objects array and you are trying to forcefully unwrap it, that's the reason for the crash.

You need to change the implementation like:

let query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects : [PFObject]?, error : NSError?) -> Void in

  self.users.removeAll(keepCapacity : true)
  if let objects = objects
  {
        for object in objects
        {
           let user : PFUser = (object as? PFUser)!
            self.users.append(user.username!)
        }
   }
   self.tableView.reloadData()
})
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • With your help I fixed the bug. It let me to another bug. I keep getting `invalid token` errors. I'll try to find a solution to this problem. – Divine Davis Nov 08 '15 at 09:37
  • 1
    @DivineDavis: Thanks for your comment. I'm not familiar with parse and don't know why those session error is coming. May be this answer can help you to understand the issue : http://stackoverflow.com/questions/29423344/parse-invalid-session-token-code-209-version-1-7-1 Happy Coding !!! – Midhun MP Nov 08 '15 at 09:39