0
点赞
收藏
分享

微信扫一扫

swift 数组切片 arraySlice


ArraySlice时数组或者其他ArraySlice的一段连续切片,和原数组共享内存。

当要改变AraySlice的时候,ArraySlice会copy出来,行程单独内存,

ArraySlice 拥有和Array基本完全类似的方法

//: A UIKit based Playground for presenting user interface

import UIKit

let colors = ["Neutral","white","Black","Orange","Red","Yellow"]
let slice:ArraySlice=colors[1...5]
print(slice)

["white", "Black", "Orange", "Red", "Yellow"]

可以通过Drop 得到ArraySlice

//: A UIKit based Playground for presenting user interface

import UIKit
let array=[Int](1...20)
array.dropFirst()
array.dropFirst(3)
array.dropLast()
array.dropLast(3)
print(array.drop{ $0 < 10 })
print(array)

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

 

举报

相关推荐

0 条评论