简体   繁体   English

具有CollectionType的Swift协议和协议扩展

[英]Swift Protocols and Protocols Extensions With CollectionType

I don't understand the return type for an array.indexOf() in swift. 我不明白swift中array.indexOf()的返回类型。 When I command click the function it takes me to a protocol extension: 当我命令单击该函数时,它将带我到协议扩展:

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

The method indexOf returns Self.Index?, indexOf方法返回Self.Index ?,

how does it know that its an Int? 它怎么知道它是一个Int?

If you look into Swift header for _CollectionDefaultsType, you will see the protocol definition as follows, 如果查看_CollectionDefaultsType的Swift标头,则会看到以下协议定义,

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

If you go through the Swift header file, you can see the definition of Array as follows 如果浏览Swift头文件,则可以看到Array的定义,如下所示

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

The getter startIndex, endIndex, first are the ones which are implemented from the protocol _CollectionDefaultsType , whose type is Self.Index . getter startIndex,endIndex首先是从协议_CollectionDefaultsType实现的,其类型为Self.Index Now, if you look at the definition of the indexOf method, it is implemented as a protocol extension with the type of 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?
}

Thus, the type Index gets inferred to Int from above two implementation. 因此,从上面的两个实现将类型Index推断为Int。

By the way, if you type into playground to see the type Index inside Array, typing Array.Index, the autocomplete shows the type as Int, 顺便说一句,如果您输入Playground以查看Array中的Index类型,并键入Array.Index,则自动完成功能会将类型显示为Int,

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM