Swift Array Slice

1.相遇

有个业务需求,把表格中的数据整合到一个数组中,如下

Name Age Item1 Item2 Item3 Country
Jack 21 32 43 null China
Tom 23 83 67 75 US

整合后的结果是这样

[
    {
        "Name": "Jack",
        "Age": 21,
        "score": [
            {
                "Item1": 32
            },
            {
                "Item2": 43
            }
        ],
        "Country": "China"
    },
    {
        "Name": "Tom",
        "Age": 23,
        "score": [
            {
                "Item1": 83
            },
            {
                "Item2": 67
            },
            {
                "Item3": 75
            }
        ],
        "Country": "US"
    }
]

有个中间的状态,就是把一条记录整合出来之后,取中间的几个元素作为一个整体,所以需要把中间的几个数据切片到一个新的数组中。
之前在学习Swift的时候,了解过一下数组的用法,以为是非常了解了,实则不然。

2.对于切片数组的 subscript ,你了解多少?

一个简单例子

let array = ["A", "B", "C", "D"]
let slicedArray = array[1..<3]
print(slicedArray.count) // 2
print(slicedArray[0]) // error

WHY?

明明数组中有两个元素,但是为什么用subscript访问,却报错呢?
还有

let slicedArray String = slicedArray.reduce("start: ") { (acc, item) -> String in
    "\(acc)\(item)"
}
print(slicedString) // start: BC

很明显这个"数组"中是有数据的呀,怎么就是取不出来呢?😢😢😢

3.startIndex & endIndex

slicedArray 的 startIndex 和 endIndex 是多少呢?

print(slicedArray.startIndex) // 1 instead 0
print(slicedArray.endIndex) // 3

所以,在用[0]访问slicedArray的时候,其实是越界的 out of bounds !!!!

4.怎么解决这个问题

不去详细的解释为什么,这个跟swift对数组的设计理念相关,copy-on-write,目的之一提高性能。另外,一个需要注意的地方,就是在官方的开发文档上,有这样一段说明:

Warning: Long-term storage of ArraySlice instances is discouraged.

Because a ArraySlice presents a view onto the storage of some larger array even after the original array's lifetime ends, storing the slice may prolong the lifetime of elements that are no longer accessible, which can manifest as apparent memory and object leakage. To prevent this effect, use ArraySlice only for transient computation.

警告:不建议长时间的存储ArraySlice实例

由于一个ArraySlice实例呈现的是某个比较大的数组上的一个视图。如果这个原始数组已经结束了其生命周期,存储这个切片实例可能会延长那些不能再被访问的数组元素的存活时间,这就造成了明显的内存和对象泄露。为了防止这个影响的发生,只在临时性的计算时,使用 ArraySlice

两个方案解决这个问题:

  1. 违背swift的设计原则 如果想长期的保留这个切片对象,请转化成数组

    直接创建一个新的数组变量

let lastedArray = Array(array[1..<3])
print(lastedArray.startIndex) // 0
print(lastedArray.endIndex) // 2
print(lastedArray[0]) // B
  1. 使用数组的属性startIndex,endIndex
combinedArray[combinedArray.startIndex] // B
combinedArray[combinedArray.startIndex.advancedBy(1)] // C

5.参考文献

  1. swift-subarrays-array-and-arrayslice
  2. swift-array-and-arrayslice-xcode-7.2

推荐阅读更多精彩内容

  • 53.计算字符 在字符串中获取字符值的数量, 可以使用字符串字符属性中的计数属性: let unusualMena...
    无沣阅读 911评论 0 4
  • 基础部分(The Basics) 当推断浮点数的类型时,Swift 总是会选择Double而不是Float。 结合...
    gamper阅读 991评论 0 7
  • Chapters: The Basics - Properties Excerpt From: Apple Inc...
    碧波浮沉阅读 496评论 0 1
  • 今晚坐爸爸的车回家,看着相反的道上不时开过来一辆车 起初我想 要是没车开过来就好了,爸爸就不用担心撞车问题了,可以...
    3500f3ef60b3阅读 126评论 0 1
  • 一直一个人夜跑,偶尔约个朋友。两人小组今天急剧膨胀到九个人。队伍发展速度远远超过预期。其中一个队友竟然还是天天同一...
    prettycat阅读 62评论 0 2