2015-06-21 238 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协议章节不讨论这一点。 –