闭包参数由swift中嵌套闭包内的位置引用

时间:2016-01-27 16:52:08

标签: ios swift closures

我有这段代码可以正常工作:

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

我想知道是否有任何方法可以在event内以某种方式引用find

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

使其成为单行?

1 个答案:

答案 0 :(得分:6)

不,这是不可能的。内部$0遮蔽外部$0,并且没有其他方式可以引用外部范围中的import org.apache.jmeter.services.FileServer; if (ResponseCode != null && ResponseCode.equals("200") == false) { Failure = true; // displays in Results Tree FailureMessage ="Creation of a new CAE record failed. Response code " + ResponseCode + "." ; // Static elements part1 = "Creation of a new record failed. Response code: "; part2 = ". Sorry!"; // Open File(s) FileOutputStream f = new FileOutputStream("d:\\error.csv", true); PrintStream p = new PrintStream(f); // Write data to file p.println( part1 + ResponseCode + part2 ); // Close File(s) p.close(); f.close(); }

正如@DavidBerry所提到的,该语法适用于简短匿名函数。否则事情会变得非常混乱。