Thijs
Thijs

Reputation: 717

findObjectsInBackgroundWithBlock block signature not correct

I'm trying to fetch a bunch of rows with this code

var query = PFQuery(className:"Test")
query.findObjectsInBackgroundWithBlock {
    (objects: NSArray?, error: NSError?) in

    // do something
}

But it doesn't compile, I get the following error:

Cannot invoke 'findObjectsInBackgroundWithBlock' with an argument list of type '((NSArray?, NSError?) -> _)'

In the docs I find this:

block: The block to execute. It should have the following argument signature:
^(NSArray *objects, NSError *error)

Which seems to be pretty similar to me. What am I doing wrong?

Upvotes: 2

Views: 1155

Answers (5)

Thijs
Thijs

Reputation: 717

Found the correct syntax in another post. Nothing wrong with the arguments list or wrapping/unwrapping the objects; instead, the closure had to be passed as an argument to findObjectsInBackgroundWithBlock. It works when written like this:

query.findObjectsInBackgroundWithBlock( { (NSArray results, NSError error) in
    // do something
})

Upvotes: 0

Paulo
Paulo

Reputation: 612

It's basically saying that the two question marks are no longer needed.

findObjectsInBackgroundWithBlock {(objects: NSArray?, error: NSError?)

Becomes

findObjectsInBackgroundWithBlock {(objects: NSArray, error: NSError)

If I am wrong, can someone correct me, but this is what I have noticed from Google etc.

Upvotes: 0

Brian R Moore
Brian R Moore

Reputation: 41

I am having the same issue. Are you using swift1.2? if so, i think it has something to do with the unwrapping "!". try changing your "!" to "?"

Upvotes: 4

aldominium
aldominium

Reputation: 291

Have you tried

query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
//code
}

Upvotes: 0

Uttam Sinha
Uttam Sinha

Reputation: 722

Try this -

(objects:[AnyObject]!, error: NSError!) in

Then cast your [AnyObject] to a [PFObject]

let myObjects = objects as? [PFObject]

Upvotes: 2

Related Questions