在数组上映射一个void(非返回函数) [英] Map a void (non-returning function) over an array

查看:151
本文介绍了在数组上映射一个void(非返回函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是不言而喻的,有没有任何方法映射,例如, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆