原文鏈接:《Advanced Swift》筆記4:切片(Slice)
切片(Slice)是基于任何集合類型(遵守CollectionType的類型)的輕量級(jí)封裝,默認(rèn)的實(shí)現(xiàn)是返回了一個(gè)對(duì)原來(lái)集合的封裝,再加上一個(gè)索引的子范圍,所以它的內(nèi)存大小會(huì)比原來(lái)更大。而且包括Swift的數(shù)組和字符串在內(nèi)的很多可切片的容器,切片和原集合共享存儲(chǔ)緩存,這會(huì)導(dǎo)致即使原集合離開(kāi)了作用域,切片依然會(huì)持有原集合的緩存,這可能會(huì)導(dǎo)致內(nèi)存問(wèn)題。
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.
另外,因?yàn)榍衅淖兞怂饕秶?,因此我們不能默認(rèn)其索引是從0開(kāi)始,而應(yīng)該是從其startIndex開(kāi)始,這也是為什么在Swift中應(yīng)該用 for in 循環(huán),而棄用C風(fēng)格的for循環(huán)的原因之一。
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
// 因?yàn)閟ubArray占用的內(nèi)存比原集合更大
sizeofValue(array) // 8
sizeofValue(subArray) // 32
// subArray持有原集合,只是改變了索引范圍
// 所以直接以數(shù)字為索引,很容易出錯(cuò)。
// 標(biāo)準(zhǔn)的索引:
subArray[subArray.startIndex.advancedBy(0)]
subArray[0] //這一句會(huì)報(bào)錯(cuò),因?yàn)閟ubArray.startIndex 是4,0越界了。
subArray.count
// 這也是Swift中棄用傳統(tǒng)C風(fēng)格的for循環(huán),改用for in循環(huán)的原因之一。
for int in subArray {
print(int)
}