0
votes

I am trying to implement search function in my app that uses Parse and want to use reload data to sort PFObjects. My app can do reloadData() first time, but it returns "unexpectedly found nil while unwrapping an Optional value" after the first time. I have tried conducting reloadData in the mainThread and removing weak reference in my collectionView, but it still does not work. I attached my code below. Thank you in advance!

@IBOutlet var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    loadCollectionViewData()
}

//This method is also conducted when the user taps search button
func loadCollectionViewData(){
    let ud = NSUserDefaults.standardUserDefaults()

    var query = PFQuery(className: "Posts")


    if ud.objectForKey("searchKeyFromVCKey") != nil{
        //This is keyword the user puts in
        var searchKey = ud.objectForKey("searchKeyFromVCKey") as! String
        println("searchKey \(searchKey)")

        if searchKey != "" {
            //If a user is searching something...
            query.whereKey("searchTag", containsString: searchKey.lowercaseString)
        }}

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in


        if error == nil {

           postObject.removeAll(keepCapacity: false)

            if let object = objects as? [PFObject] {
                postObject = object


            }

            self.collectionView.reloadData() //This works the first time when I conduct loadCollectionViewData() in ViewDidLoad

        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")


        }

    }
    ud.removeObjectForKey("searchKeyFromVCKey")

}
2
Which reference is unexpectedly nil?Aaron Brager
@AaronBrager I don't know how to find which reference is unexpectedly nil. With this code, self.collectionView.reloadData() gets highlighted in green, and the error occurs.Emily
Change postObject = object into postObject = object!DDPWNAGE
@DDPWNAGE When I try to do that, Xcode shows an error "Operand of postfix '!' should have optional type; type is '[PFObject]'"Emily
@Emily Alright. Add an if (object == nil) statement in there.DDPWNAGE

2 Answers

0
votes

Maybe the reason is here:

println("Error: \(error!) \(error!.userInfo!)")

userInfo is an optional value, Are U sure it's not a nil value?

The best way to find the reason is to enable Exception Breakpoint, and then we will get more crash infos to help us solve it.

0
votes

If you get the error on this line...

self.collectionView.reloadData()

.. then either self or collectionView could be nil. To check this just place a breakpoint on this line and check which is nil - either self or collectionView. If you do not know how to set breakpoints and check values in the debugger then please search for tutorials - it's easy, but to much to explain here. You will have to learn this anyway if you want to continue programming.

And: removing the "weak" from the collectionView is a bad idea because it could lead to a reference cycle and a memory leak.