iOS中块(Objective-C)和闭包(Swift)之间的区别 [英] Difference between block (Objective-C) and closure (Swift) in iOS

查看:81
本文介绍了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."

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

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