let paths = Array(0..<ct).map {NSIndexPath(forRow:$0, inSection:sec)}
实际上,
map
是
CollectionType
的一个实例方法,
Range
本身是个
CollectionType
。因此,
我不需要转换为数组:
let paths = (0..<ct).map {NSIndexPath(forRow:$0, inSection:sec)}
filter
实例方法也会生成一个新数组。新数组中的每个元素都是老数组中的,顺序也相
同;不过,老数组中的一些元素可能会被去除,它们被过滤掉了。起过滤作用的是你所
提供的函数;它接收一个元素类型的参数并返回一个
Bool
,表示这个元素是否应该被放
到新数组中。
比如:
let pepboys = ["Manny", "Moe", "Jack"]
let pepboys2 = pepboys.filter{$0.hasPrefix("M")} // [Manny, Moe]
最后来看看
reduce
实例方法。如果学过
LISP ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.