【问题标题】:unable to find enumerate() func in Swift standard library reference在 Swift 标准库参考中找不到 enumerate() 函数
【发布时间】:2016-03-11 02:07:23
【问题描述】:

我是 Swift 新手,正在学习 Array 的概念。我从“swift 编程语言 2.1”中看到了下面的代码。

var array = [1,2,3,4,5]
for (index, value) in array.enumerate() {
    print("\(value) at index \(index)")
}

我想了解更多关于enumerate() 函数的信息,所以我查找了Apple developer's page on Array,但是,我在此页面上找不到名为enumerate() 的函数。我是在看错地方还是遗漏了什么?有人可以帮帮我吗?提前感谢您的帮助!

【问题讨论】:

    标签: ios arrays swift enumerate


    【解决方案1】:

    当您遇到找不到文档的 Swift 标准库函数或方法时,请在 Xcode 中按命令单击它。这将带您了解它的定义,在这种情况下是

    extension SequenceType {
        /// Return a lazy `SequenceType` containing pairs (*n*, *x*), where
        /// *n*s are consecutive `Int`s starting at zero, and *x*s are
        /// the elements of `base`:
        ///
        ///     > for (n, c) in "Swift".characters.enumerate() {
        ///         print("\(n): '\(c)'")
        ///       }
        ///     0: 'S'
        ///     1: 'w'
        ///     2: 'i'
        ///     3: 'f'
        ///     4: 't'
        @warn_unused_result
        public func enumerate() -> EnumerateSequence<Self>
    }
    

    上述说明enumerate() 为您的集合中的每个值返回一个元组,元组中的第一个元素是当前项目的索引,第二个是该项目的值。

    【讨论】:

    • 非常感谢您的提示!顺便说一句,您介意我问,为什么文档中没有包含某些功能或方法吗?
    • enumerate 方法is defined by SequenceTypeArray 采用的协议(间接采用CollectionType 的方式)。一个类型的 Apple 文档没有显示它通过超类或协议一致性获得的所有方法,因此要查看某个类型的所有方法,您必须单击“继承自”和“符合”链接。
    猜你喜欢
    • 2018-07-24
    • 2021-10-10
    • 2017-09-18
    • 2012-12-12
    • 2015-11-27
    • 1970-01-01
    • 2014-07-07
    • 2010-10-05
    相关资源
    最近更新 更多