高阶函数:“不能用类型为(((_)-> _))的参数列表调用'map'". [英] Higher order function: "Cannot invoke 'map' with an argument list of type '((_) -> _)'"

查看:120
本文介绍了高阶函数:“不能用类型为(((_)-> _))的参数列表调用'map'".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用快速的高阶函数(地图)从给定的UIView.subviews数组中删除所有Subviews.线

I would like to use a swift higher order function (map) to remove all Subviews from a given UIView.subviews array. The line

(cell.contentView.subviews as [UIView]).map { $0.removeFromSuperView() }

导致错误 无法使用类型为'(((_)-> _)')的参数列表调用'map'"

causes the error "Cannot invoke 'map' with an argument list of type '((_) -> _)'"

我现在想知道编译器需要我什么.

I would like to know what the compiler needs from me at this point.

推荐答案

我会说map不适合这种操作.它基于其他sequence元素创建了一个新序列,但是您不想创建一个序列,只想遍历它们并将一个函数应用于它们.迅速地没有匹配您想要的高阶函数,我希望他们能尽快投入使用.因此,您最好的办法是使用for循环或编写自己的函数来实现所需的功能.

I would say that map is not for this kind of operation. It creates a new sequence based on an others sequences elements, but what you don't want to create a sequence, you just want to iterate through them and apply a function to them. In swift there is no higher order function that matches what you want, I hope they will put something in soon. So the best you can do is to use a for loop or write your own function which does what you want.

我建议您编写自己的functon(基于foreach的标量):

I would like to suggest to write your own functon (based on what scalas foreach is):

extension Array {

    func foreach(function: T -> ()) {
        for elem in self {
            function(elem)
        }
    }
}

使用Swift 2.0更新

查看全文

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