Difference between block (Objective-C) and closure (Swift) in iOS

Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks:

“Swift closures and Objective-C blocks are compatible, so you can pass Swift closures to Objective-C methods that expect blocks. Swift closures and functions have the same type, so you can even pass the name of a Swift function.

Closures have similar capture semantics as blocks but differ in one key way: Variables are mutable rather than copied. In other words, the behavior of __block in Objective-C is the default behavior for variables in Swift.”


Slight differences. One was mentioned; variables are captured as variables, not as values. Which can be either useful or a trap. Importantly you can define a capture list in a Swift closure, so if you include self.property in the capture list, then the value of that property is captured, and not self. That also simplifies capturing weak variables.


To show an actual code example of the differences:

This does compile:

let x : @convention(swift) (inout Int) -> ()

This does not:

let y : @convention(block) (inout Int) -> ()

with the error (inout Int) -> () is not representable in Objective-C