CountableRange的startIndex和lowerBound有什么区别?

时间:2017-05-03 19:54:00

标签: swift collections range

Documentation says

  

关联的Bound类型是CountableRange的元素和索引类型。范围的每个元素都是其自己的对应索引。 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 }

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

1 个答案:

答案 0 :(得分:5)

lowerBoundstartIndex完全等同于CountableRangeupperBoundendIndex也是如此,如文档中所述:

  

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

CountableRange提供这两个属性的唯一原因是startIndex and endIndex are required by the Collection protocollowerBound and upperBound are common to all Range types(即:CountableRange,CountableClosedRange等)。