ios中block (Objective C)和closure (Swift)的区别

时间:2022-03-17 21:29:30

In tutorials its 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".

在教程中,它的功能都是相同的,甚至闭包更容易,然后它可以避免块和内存管理的复杂性,我已经学习了很多教程,但是除了这些,我没有得到swift的“闭包”和Objective-C“block”之间的区别。

Can anybody explain this difference in deep. Any help would be appreciated.

有人能深入地解释这种差异吗?如有任何帮助,我们将不胜感激。

3 个解决方案

#1


52  

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

摘自:苹果公司"使用Swift与可可和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.

“快速闭包和Objective-C模块是兼容的,因此您可以将快速闭包传递给预期块的Objective-C方法。Swift闭包和函数具有相同的类型,因此您甚至可以传递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.”

闭包的捕获语义与块相似,但有一个关键的区别:变量是可变的,而不是复制的。换句话说,Objective-C中__block的行为是Swift中变量的默认行为。

#2


2  

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.

轻微的差异。一个被提及;变量被捕获为变量,而不是值。它可以是有用的,也可以是陷阱。重要的是,您可以在快速闭包中定义捕获列表,因此如果您包含self。捕获列表中的属性,然后捕获该属性的值,而不是self。这也简化了捕捉弱变量。

#3


1  

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

对于error (inout Int) ->()在Objective-C中不可表示

#1


52  

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

摘自:苹果公司"使用Swift与可可和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.

“快速闭包和Objective-C模块是兼容的,因此您可以将快速闭包传递给预期块的Objective-C方法。Swift闭包和函数具有相同的类型,因此您甚至可以传递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.”

闭包的捕获语义与块相似,但有一个关键的区别:变量是可变的,而不是复制的。换句话说,Objective-C中__block的行为是Swift中变量的默认行为。

#2


2  

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.

轻微的差异。一个被提及;变量被捕获为变量,而不是值。它可以是有用的,也可以是陷阱。重要的是,您可以在快速闭包中定义捕获列表,因此如果您包含self。捕获列表中的属性,然后捕获该属性的值,而不是self。这也简化了捕捉弱变量。

#3


1  

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

对于error (inout Int) ->()在Objective-C中不可表示