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

5
  • 2
    have you looked into the error object? The error and the optional type of the object are there for a reason: to tell you that something might go wrong and for you to able to look into and recover from it! Commented Nov 8, 2015 at 9:13
  • It's hard for me to find the reason for the error Commented Nov 8, 2015 at 9:14
  • Where do you declare/get objects? Commented Nov 8, 2015 at 9:16
  • 2
    why? what does the error object tell you? Maybe it has something to do with the printed error invalid session token!? Commented Nov 8, 2015 at 9:16
  • @Arc676 its a parameter of the block Commented Nov 8, 2015 at 9:16

1 Answer 1

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() }) 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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 : stackoverflow.com/questions/29423344/… Happy Coding !!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.