Parse.com query returning nil for object and crashing app

267 views Asked by At

This should normally be easy and straight forward but I'm struggling to find an answer. I want to be able to assign the objectId to an array so when I want to update a column I can use query.getObjectInBackgroundWithId. I'm performing the initial query and assigning the values found to a dictionary. Unfortunately the app crashes when I try to assign the column objectId and I get fatal error: unexpectedly found nil while unwrapping an Optional value

var query = PFQuery(className:"classname")
    query.whereKey("available", equalTo:true)
    query.findObjectsInBackgroundWithBlock( {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            println("Successfully retrieved \(objects!.count)")

            if let objects = objects as? [PFObject] {
                for object in objects {

                    let x = dict(aColumn1: (object["column1"] as! String), aColumn2: (object["column2"] as! String), aId: (object["objectId"] as! String))

                    dictArr.append(x)

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

There are 1 answers

1
Icaro On

Try to create your array like this:

let x = [(object["column1"] as! String),(object["column2"] as! String), (object["objectId"] as! String)]

But seems more like you want a dictionary instead, if that is the case you can try this:

let x = [("aColumn1": (object["column1"] as! String), "aColumn2": (object["column2"] as! String), "aId": (object["objectId"] as! String))]