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:
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.
As @DavidBerry mentions, that syntax is intended for short anonymous functions. Things could get very confusing otherwise.
链接地址: http://www.djcxy.com/p/31786.html