iOS中块(Objective-C)和闭包(Swift)之间的区别

iOS中块(Objective-C)和闭包(Swift)之间的区别

问题描述:

在教程中,虽然闭包比块更容易,并且避免了块和内存管理的复杂性,但在功能上都是相同的,我已经看过许多教程,但是除了这些,我没有明白swift的关闭"和Objective-C的阻止".

In tutorials it's written that functionally both are same even closure is more easier then block and its avoided the complexity of block and memory management, I've gone through many tutorials but except these I'm not getting the difference between swift's "closure" and Objective-C "block".

摘录自:Apple Inc.将Swift与Cocoa和Objective-C结合使用." iBooks:

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

"Swift闭包和Objective-C块是兼容的,因此您可以将Swift闭包传递给需要块的Objective-C方法. Swift闭包和函数具有相同的类型,因此您甚至可以传递Swift函数的名称.

"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.

闭包具有与块类似的捕获语义,但是在一个关键方面有所不同:变量是可变的,而不是复制的.换句话说,Objective-C中__block的行为是Swift中变量的默认行为."

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."