ios中块(目标C)和关闭(Swift)之间的差异 [英] Difference between block (Objective C) and closure (Swift) in ios

查看:104
本文介绍了ios中块(目标C)和关闭(Swift)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

任何人都可以深入解释这种差异。
任何帮助都会感激。

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

推荐答案

摘录自Apple Inc.将Swift与Cocoa和Objective-C 。iBooks:

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


Swift闭包和Objective-C块是兼容的,所以你可以将Swift闭包传递给Objective-块。 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."

这篇关于ios中块(目标C)和关闭(Swift)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆