简体   繁体   English

快速嵌套嵌套闭包中的位置引用的闭包参数

[英]Closure arguments referenced by position inside nested closure in swift

I have this piece of code which works fine: 我有一段正常工作的代码:

var discoverEvents = events.map { event in
   userEvents.find { $0.eventID == event.eventID } ?? event
}

I wonder if there is any way to reference event by position inside find somehow like that: 我不知道是否有什么办法可以参考event的位置内find莫名其妙地这样:

var discoverEvents = events.map { userEvents.find { $$0.eventID == $0.eventID } ?? $0 }

to make it one-liner? 使它成为单线?

No, it's not possible. 不,不可能。 The inner $0 shadows the outer one, and there is no other way to refer to the $0 in the outer scope. 内部$0遮蔽了外部$0 ,没有其他方法可以在外部范围中引用$0

As @DavidBerry mentions, that syntax is intended for short anonymous functions. 正如@DavidBerry提到的那样,该语法旨在用于简短的匿名函数。 Things could get very confusing otherwise. 否则事情会变得很混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM