CountableRange的startIndex和Lower Bound有什么区别?



文档 说:

关联的绑定类型是可计数范围的元素和索引类型。范围的每个元素都是其自己的相应索引。CountableRange 实例的下限是其起始索引,上限是其结束索引。

声明var lowerBound: Bound { get }

var lowerBound: Bound
The range's lower bound.
In an empty range, lowerBound is equal to upperBound.
Declaration
var lowerBound: Bound { get }

声明var startIndex: Bound { get }

var startIndex: Bound
The position of the first element in a nonempty collection.
If the collection is empty, startIndex is equal to endIndex.
Declaration
var startIndex: Bound { get }

这两者之间有什么区别吗,我如何决定使用哪一个?

lowerBoundstartIndex对于一个CountableRange是完全等价的,就像upperBoundendIndex一样,如文档所述:

CountableRange 实例的下限是其起始索引,上限是其结束索引。

CountableRange提供这两个属性的唯一原因是Collection协议需要startIndexendIndex,而lowerBoundupperBound是所有范围类型(即:CountableRange,CountableClosedRange等)共有的。

最新更新