在数组上映射一个void(非返回函数) [英] Map a void (non-returning function) over an array
问题描述
addGestureRecognizer
在 UIGestureRecognizers
。我一直在摆脱各种不同的 recognitionizers.map(MyWebOutlet.addGestureRecognizer)
但是由于内置地图必须返回另一个数组,需要一个返回某种值的函数。我应该在另一个返回的函数中包装 addGestureRecognizer
还是另一个更清晰的方法?
映射
被设计为从已经给出了一个给定的变换的另一个数组返回一个数组(通过转换每个元素)。因此,您传递给它的函数必须返回已转换的元素。
在您的情况下,您实际上并没有转换元素,您只需要使用它们在一个函数的论证中。为此,您可以使用 forEach
。
recognitionizers.forEach MyWebOutlet.addGestureRecognizer)
Title is self-explanatory, is there any way of mapping, for example, addGestureRecognizer
over an array of UIGestureRecognizers
. I've been fiddling around with variations of
recognizers.map(MyWebOutlet.addGestureRecognizer)
but since built-in map has to return another array in requires a function that returns some kind of value. Should I just wrap addGestureRecognizer
in another returning function or is the another cleverer way?
map
is designed to return an array from another array that has had a given transformation applied to it (by transforming each element). Therefore naturally the function that you pass to it has to return the transformed element.
In your case, you're not actually transforming the elements, you just want to use them in an argument of a function. To do this, you can use forEach
.
recognizers.forEach(MyWebOutlet.addGestureRecognizer)
这篇关于在数组上映射一个void(非返回函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!