ios - Swift 中闭包中的 $0 代表什么?

标签 ios xcode swift closures

我见过 Swift 中的闭包在内部使用 $0,有时使用 $1$0 到底是什么?您还可以使用哪些其他 $X

以下是它的使用示例:

applyMutliplication(2, {$0 * 3})
array.map({$0 + 1})

最佳答案

这是一个简写参数名称。

来自 Swift 书籍:

“Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.”

— Apple Inc. “The Swift Programming Language.”

它有助于减少代码的冗长(有时以可读性为代价),因此您在定义闭包时不必写出长参数列表。

关于ios - Swift 中闭包中的 $0 代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312973/

相关文章:

iOS 8 Touch ID 损坏

ios - 带有图像的 UITableViewCell 动态高度

objective-c - 锁定一个对象不被多个线程访问 - Objective-C

ios - 初始化方法的参数具有默认值时的问题

ios - 如何使 iOS 中的 PhoneGap 状态栏变为半透明?

objective-c - 将枚举值分配给 Obj-C 中的属性

android - 在 android 和 ios 应用程序中显示图像的最可行方法

xcode - 使用 SwiftUI 在 macOS 上阻止 FirebaseApp.configure() 的应用沙盒

ios - 强制模拟位置在 XCode 6.4 中固定

swift 核心数据 : accessing relationship is slower than just fetching by predicate - is it designed that way?