切片(Slice)是基于任何集合类型(遵守CollectionType的类型)的轻量级封装,默认的实现是返回了一个对原来集合的封装,再加上一个索引的子范围,所以它的内存大小会比原来更大。而且包括Swift的数组和字符串在内的很多可切片的容器,切片和原集合共享存储缓存,这会导致即使原集合离开了作用域,切片依然会持有原集合的缓存,这可能会导致内存问题。
With many sliceable containers, including Swift’s arrays and strings, a slice shares the storage buffer of the original collection. This has an unpleasant side effect: slices can keep the original collection’s buffer alive in its entirety, even if the original collection falls out of scope. If you read a 1 GB file into an array or string, and then slice off a tiny part, the whole 1 GB buffer will stay in memory until both the collection and the slice are destroyed.
另外,因为切片改变了索引范围,因此我们不能默认其索引是从0开始,而应该是从其startIndex开始,这也是为什么在Swift中应该用 for in 循环,而弃用C风格的for循环的原因之一。
1 | let array = [1, 4, 5, 2, 11, 34, 33, 88, 43] |