arrays - XCode 7 Beta 3 - 数组扩展

标签 arrays swift xcode7-beta3

XCode 7 Beta 3 之前的旧版本代码:

extension Array {
    func filterByIndex<S: SequenceType where S.Generator.Element == Int>(indices: S) -> [T] {
        return Array(PermutationGenerator(elements: self, indices: indices))
    }

    func find(includedElement: T -> Bool) -> Int? {
        for (idx, element) in self.enumerate() {
            if includedElement(element) {
                return idx
            }
        }
        return nil
    }

}

XCode 7 Beta 3 之后的新版本代码:

extension Array {
    func filterByIndex<S: SequenceType where S.Generator.Element == Int>(indices: S) -> [Element] {
        return Array(PermutationGenerator(elements: self, indices: indices))
    }

    func find(includedElement: Element -> Bool) -> Int? {
        for (idx, element) in self.enumerate() {
            if includedElement(element) {
                return idx
            }
        }
        return nil
    }
}

但是现在,当我编写此行时,函数 filterByIndex 给出了一个错误:

let names = (namesArr as! [String]).filterByIndex(dupes)

“[String]”没有名为“filterByIndex”的成员

改变了什么?

最佳答案

新版本的代码对我来说工作得很好:

[ "zero", "one", "two", "three", "four" ].filterByIndex([1, 3])
// result: [ "one", "three" ]

我假设您遇到的问题是在其他地方。我最初怀疑 dupes 的类型(其定义未显示)与通用函数的要求不匹配,但在我的测试中,在这种情况下错误消息应该不同。

关于arrays - XCode 7 Beta 3 - 数组扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352437/

相关文章:

arrays - node.js 循环数组写入文件

arrays - 有没有一种简单的方法可以在 Fsharp 中将 byte [] 转换为 ReadOnlySpan<byte> ?

ios - 核心位置的动态精度

swift - 没有这样的模块锁匠

swift - 使用 Xcode Playground captureValue()

JavaScript concat 没有按预期工作,需要详细说明吗?

java - 将 char 数组分配给 int 数组

Swift 4 Firebase 实时数据库嵌套字典

swift - 如何在mapbox-gl中显示用户位置标记?