denislexic
denislexic

Reputation: 11362

Getting error in Xcode when doing this if let objects = objects as? [PFObject]

Just upgraded to Swift 2 and using Xcode 7. Using Parse for backend.

I'm doing a normal query in background:

let query = PFQuery(className: "User")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

    if let objects = objects as? [PFObject] {

    }
}

I get error in Xcode:

Downcast from '[PFObject]?' to '[PFObject]' only unwraps optionals; did you mean to use '!'?

Any ideas how I can fix this?

Upvotes: 0

Views: 510

Answers (2)

Arturo Jamaica
Arturo Jamaica

Reputation: 73

Just remove as? [PFObject]

Parse Change the [AnyObject] to a [PFObject]

findObjectsInBackgroundWithBlock({(objects:[AnyObject]?, error:NSError?)

so :

findObjectsInBackgroundWithBlock { (objects, error)

The Downcast is not required

Upvotes: 2

Change your code into:

let query = PFQuery(className: "User");

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

   if let objects = objects as! [PFObject] {

   }
}

Upvotes: 0

Related Questions