Shorthand Argument Names $0 : 只用于指代Closer声明中的形参
Shorthand Argument Names
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.
If you use these shorthand argument names within your closure expression, you can omit the closure’s argument list from its definition, and the number and type of the shorthand argument names will be inferred from the expected function type. The in
keyword can also be omitted, because the closure expression is made up entirely of its body:
- reversedNames = names.sorted(by: { $0 > $1 } )
Here, $0
and $1
refer to the closure’s first and second String
arguments.
https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID97
public func map<U>(_ transform: (Value) -> U) -> Result<U, Error> {
return flatMap { .success(transform($0)) }
}
指代flatMap<U>(_ transform: (Value) -> Result<U, Error>) -> Result<U, Error>中的(Value)
/// Returns the result of applying `transform` to `Success`es’ values, or re-wrapping `Failure`’s errors.
public func flatMap<U>(_ transform: (Value) -> Result<U, Error>) -> Result<U, Error> {
return analysis(
ifSuccess: transform,
ifFailure: Result<U, Error>.failure)
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步