【发布时间】:2014-09-26 05:32:12
【问题描述】:
虽然 Array struct def 是:
struct Array<T> : MutableCollection, Sliceable {
typealias Element = T
var startIndex: Int { get }
var endIndex: Int { get }
subscript (index: Int) -> T
func generate() -> IndexingGenerator<[T]>
typealias SliceType = Slice<T>
subscript (subRange: Range<Int>) -> Slice<T>
}
而 MutableCollection 是:
protocol MutableCollection : Collection {
subscript (i: Self.IndexType) -> Self.GeneratorType.Element { get set }
}
然后收藏:
protocol Collection : Sequence {
subscript (i: Self.IndexType) -> Self.GeneratorType.Element { get }
}
然后是序列:
protocol Sequence {
typealias GeneratorType : Generator
func generate() -> GeneratorType
}
和可切片:
protocol Sliceable {
typealias SliceType
subscript (_: Range<Self.IndexType>) -> SliceType { get }
}
(Slice几乎相当于Array)
后台在哪里?
【问题讨论】:
-
这是否回答了您的问题:stackoverflow.com/questions/24073269/what-is-a-slice-in-swift ? - 切片指向现有数组。它没有自己的存储空间。
-
谢谢 Martin,但是 struct Array
呢? -
后备存储可能不公开可见。它也可以用 C 而不是 Swift 实现。