Skip to Main Content
iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
book

iOS 编程基础:Swift、Xcode 和Cocoa 入门指南

by Matt Neuburg
January 2017
Intermediate to advanced content levelIntermediate to advanced
552 pages
9h 43m
Chinese
China Machine Press
Content preview from iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
218
4
let path0 = NSIndexPath(forRow:0, inSection:sec)
let path1 = NSIndexPath(forRow:1, inSection:sec)
// ...
这里有个规律!我可以通
for...in
环行值来生成
NSIndexPath
对象的数组。不过,
要是使用
map
,那么表示相同的循环就会变得更加紧凑(
ct
是段中的行数):
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.
Start your free trial

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Mars Geldard, Jonathon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787111556350