2015-06-21 234 views
0

我不明白在swift array.indexOf()的返回類型。 當我命令點擊它帶我到一個協議擴展功能:Swift協議和協議擴展與CollectionType

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? 
} 

的indexOf手動調用self.index返回的方法?

它如何知道它的詮釋?

+0

Array.startIndex返回詮釋。因此,將手動調用self.index被推斷爲int –

回答

2

如果你看看能否_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,

enter image description here

+0

我迷路怎麼這條線「VAR的startIndex:詮釋{}獲得」原來這等行「typealias指數:ForwardIndexType」轉換成int –

+0

的「typealias指數:ForwardIndexType」簡單地說,類型Index是實現「ForwardIndexType」的東西。但推理是從Array實現中的「var startIndex:Int」完成的。所以想想吧,協議或協議實現並不知道「索引」是什麼,它只是知道存在某種類型的索引,但是當實現中的一個變量被賦予一個類型爲「Int」時,所有其他「Index」的用法將被推斷爲「Int」 – Sandeep

+0

謝謝,我開始明白了。有文件可以指向我嗎?預發佈iBook Swift 2.0協議章節不討論這一點。 –