《Advanced Swift》笔记4:切片(Slice) | LiJun's Blog
切片(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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | let array = [1, 4, 5, 2, 11, 34, 33, 88, 43] let subArray = array.suffixFrom(4) // subArray改变了索引范围,即改变了startIndex和endIndex。 subArray.startIndex // 4 subArray.endIndex // 9 // 因为subArray占用的内存比原集合更大 sizeofValue(array) // 8 sizeofValue(subArray) // 32 // subArray持有原集合,只是改变了索引范围 // 所以直接以数字为索引,很容易出错。 // 标准的索引: subArray[subArray.startIndex.advancedBy(0)] subArray[0] //这一句会报错,因为subArray.startIndex 是4,0越界了。 subArray.count // 这也是Swift中弃用传统C风格的for循环,改用for in循环的原因之一。 for int in subArray { print(int) } |