如果你看看能否_CollectionDefaultsType斯威夫特頭,你會看到協議定義如下,
protocol _CollectionDefaultsType : SequenceType {
/// A type that represents a valid position in the collection.
///
/// Valid indices consist of the position of every element and a
/// "past the end" position that's not valid for use as a subscript.
typealias Index : ForwardIndexType
/// The position of the first element in a non-empty collection.
///
/// In an empty collection, `startIndex == endIndex`.
var startIndex: Self.Index { get }
/// The collection's "past the end" position.
///
/// `endIndex` is not a valid argument to `subscript`, and is always
/// reachable from `startIndex` by zero or more applications of
/// `successor()`.
var endIndex: Self.Index { get }
/// Returns the first element of `self`, or `nil` if `self` is empty.
var first: Self.Generator.Element? { get }
}
如果你去通過雨燕頭文件,你可以看到數組的定義如下
struct Array<T> : CollectionType, SequenceType, _CollectionDefaultsType, _CollectionGeneratorDefaultsType, MutableCollectionType, Sliceable, _Sliceable, _DestructorSafeContainer {
/// The type of element stored by this `Array`.
typealias Element = T
/// Always zero, which is the index of the first element when non-empty.
var startIndex: Int { get }
/// A "past-the-end" element index; the successor of the last valid
/// subscript argument.
var endIndex: Int { get }
subscript (index: Int) -> T
/// Return a *generator* over the elements.
///
/// - Complexity: O(1).
func generate() -> IndexingGenerator<[T]>
/// A type that can represent a sub-range of an `Array`.
typealias SubSlice = ArraySlice<T>
subscript (subRange: Range<Int>) -> ArraySlice<T>
}
該吸氣劑的startIndex,endIndex的,第一是從特殊的協議來實現_CollectionDefaultsType的那些,其類型爲手動調用self.index。現在,如果你看一下的indexOf方法的定義,它是作爲一個協議擴展與手動調用self.index類型。
extension CollectionType where Generator.Element : Equatable {
/// Returns the first index where `value` appears in `self` or `nil` if
/// `value` is not found.
///
/// - Complexity: O(`self.count`).
func indexOf(element: Self.Generator.Element) -> Self.Index?
}
因此,類型索引被推斷從上述兩個實施INT。
順便說一句,如果你輸入到操場上看到裏面數組類型索引,打字Array.Index,自動完成顯示類型爲INT,
Array.startIndex返回詮釋。因此,將手動調用self.index被推斷爲int –