Reputation: 579
In the following code (using the Parse library), I have:
query.findObjectsInBackgroundWithBlock({(objects: [AnyObject]?, error: NSError?) -> Void in
// yada yada
})
Is the -> Void in
necessary in the code (rather, is there something else I can use? Removing it throws errors in Xcode.)?
I am very new to Swift, so this may be a dumb question...
Upvotes: 3
Views: 3106
Reputation: 1865
This -> Void in
is saying the closure is not returning anything and what follows the in
is the body of the closure. The -> Void
can be removed because Swift is very good at inferring types. The in
is necessary.
The start of the closure’s body is introduced by the
in
keyword. This keyword indicates that the definition of the closure’s parameters and return type has finished, and the body of the closure is about to begin.
You can go read more about closures here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
Upvotes: 0
Reputation: 536028
The in
line is how you get the parameters into the block:
(objects: [AnyObject]?, error: NSError?) -> Void in
If the types are known in some other way, you can omit them:
objects, error in
But you cannot omit the in
line entirely unless you pick up the parameters in some other way in your code (as $0
and $1
), and that would make your code difficult to understand. It's better to keep the in
line so you know what the parameters are.
Upvotes: 3