如何在Swift中扩展类型化数组?

myt*_*thz 191 arrays swift

如何使用自定义功能工具扩展Swift Array<T>T[]类型?

浏览Swift的API文档显示,Array方法是其扩展T[],例如:

extension T[] : ArrayType {
    //...
    init()

    var count: Int { get }

    var capacity: Int { get }

    var isEmpty: Bool { get }

    func copy() -> T[]
}
Run Code Online (Sandbox Code Playgroud)

复制和粘贴相同的源并尝试任何变化时,例如:

extension T[] : ArrayType {
    func foo(){}
}

extension T[] {
    func foo(){}
}
Run Code Online (Sandbox Code Playgroud)

它无法构建错误:

标称类型T[]不能扩展

使用完整类型定义失败Use of undefined type 'T',即:

extension Array<T> {
    func foo(){}
}
Run Code Online (Sandbox Code Playgroud)

而且它也失败,Array<T : Any>Array<String>.

好奇Swift让我扩展一个无类型数组:

extension Array {
    func each(fn: (Any) -> ()) {
        for i in self {
            fn(i)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它让我打电话给:

[1,2,3].each(println)
Run Code Online (Sandbox Code Playgroud)

但我无法创建一个正确的泛型类型扩展,因为当它流经方法时类型似乎丢失了,例如尝试用以下代码替换Swift的内置过滤器:

extension Array {
    func find<T>(fn: (T) -> Bool) -> T[] {
        var to = T[]()
        for x in self {
            let t = x as T
            if fn(t) {
                to += t
            }
        }
        return to
    }
}
Run Code Online (Sandbox Code Playgroud)

但编译器将其视为无类型,它仍允许使用以下命令调用扩展:

["A","B","C"].find { $0 > "A" }
Run Code Online (Sandbox Code Playgroud)

当使用调试器逐步执行时指示类型是,Swift.String但是尝试像String一样访问它而不String首先将其转换为构建错误,即:

["A","B","C"].find { ($0 as String).compare("A") > 0 }
Run Code Online (Sandbox Code Playgroud)

有谁知道创建类似于内置扩展的类型化扩展方法的正确方法是什么?

And*_*ber 265

对于使用扩展类型化数组,下面适用于我(Swift 2.2).例如,对类型化数组进行排序:

class HighScoreEntry {
    let score:Int
}

extension Array where Element:HighScoreEntry {
    func sort() -> [HighScoreEntry] {
      return sort { $0.score < $1.score }
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试使用structtypealias执行此操作将产生错误:

Type 'Element' constrained to a non-protocol type 'HighScoreEntry'
Run Code Online (Sandbox Code Playgroud)

更新:

要使用非类扩展类型化数组,请使用以下方法:

typealias HighScoreEntry = (Int)

extension SequenceType where Generator.Element == HighScoreEntry {
    func sort() -> [HighScoreEntry] {
      return sort { $0 < $1 }
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift 3中,某些类型已被重命名:

extension Sequence where Iterator.Element == HighScoreEntry 
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)


myt*_*thz 62

经过一段时间尝试不同的东西后,解决方案似乎<T>从签名中删除了:

extension Array {
    func find(fn: (T) -> Bool) -> [T] {
        var to = [T]()
        for x in self {
            let t = x as T;
            if fn(t) {
                to += t
            }
        }
        return to
    }
}
Run Code Online (Sandbox Code Playgroud)

现在可以正常工作而不会出现构建错误:

["A","B","C"].find { $0.compare("A") > 0 }
Run Code Online (Sandbox Code Playgroud)

  • @Palimondo不,不是,[内置过滤器执行两次回调](http://stackoverflow.com/q/24025633/85785). (5认同)
  • 我知道了.对我来说双重过滤看起来很麻烦...但是它仍然认为`过滤器'在功能上与你的`find`相当**,即函数的结果是相同的.如果您的过滤器闭合有副作用,您可能肯定不喜欢结果. (4认同)
  • 我们似乎在争论*functional*这个词的定义.通常,在函数式编程范例中,`filter`,`map`和`reduce`函数源自,函数执行其返回值.相比之下,您在上面定义的`each`函数是为其副作用执行的函数的示例,因为它不返回任何内容.我想我们可以同意当前的Swift实现并不理想,文档也没有说明它的运行时特性. (4认同)
  • @Palimondo确切地说,默认过滤器具有意外行为,而上述查找impl按预期工作(以及它存在的原因).如果它执行闭包两次,它在功能上是等价的,这可能会改变作用域变量(碰巧是我遇到的bug,因此关于它的行为的问题).还要注意这个问题,特别提到想要取代Swift的内置"过滤器". (2认同)

Dmi*_*try 11

扩展所有类型:

extension Array where Element: Comparable {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

扩展一些类型:

extension Array where Element: Comparable & Hashable {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

扩展特定类型:

extension Array where Element == Int {
    // ...
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*ard 8

我有一个类似的问题 - 希望用swap()方法扩展一般数组,该方法应该采用与数组相同类型的参数.但是如何指定泛型类型?我通过反复试验发现以下情况有效:

extension Array {
    mutating func swap(x:[Element]) {
        self.removeAll()
        self.appendContentsOf(x)
    }
}
Run Code Online (Sandbox Code Playgroud)

它的关键是"元素"这个词.请注意,我没有在任何地方定义此类型,它似乎自动存在于数组扩展的上下文中,并引用数组元素的任何类型.

我不是100%肯定那里发生了什么,但我认为这可能是因为'Element'是阵列的关联类型(请参阅此处的'关联类型' https://developer.apple.com/library/ios/documentation /Swift/Conceptual/Swift_Programming_Language/Generics.html#//apple_ref/doc/uid/TP40014097-CH26-ID189)

但是,我在Array结构参考中找不到任何引用(https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_Array_Structure/index.html#//apple_ref/swift/struct/s:Sa)...所以我还是有点不确定.


Enc*_*PTL 5

如果您想了解有关扩展数组和其他类型的内建类的信息,请参见此github存储库https://github.com/ankurp/Cent

从Xcode 6.1开始,扩展数组的语法如下

extension Array {
    func at(indexes: Int...) -> [Element] {
        ... // You code goes herer
    }
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 5

使用Swift 2.2:尝试从字符串数组中删除重复项时遇到了类似的问题.我能够在Array类上添加一个扩展,它正是我想做的事情.

extension Array where Element: Hashable {
    /**
     * Remove duplicate elements from an array
     *
     * - returns: A new array without duplicates
     */
    func removeDuplicates() -> [Element] {
        var result: [Element] = []
        for value in self {
            if !result.contains(value) {
                result.append(value)
            }
        }
        return result
    }

    /**
     * Remove duplicate elements from an array
     */
    mutating func removeDuplicatesInPlace() {
        var result: [Element] = []
        for value in self {
            if !result.contains(value) {
                result.append(value)
            }
        }
        self = result
    }
}
Run Code Online (Sandbox Code Playgroud)

将这两个方法添加到Array类允许我调用数组中的两个方法之一并成功删除重复项.请注意,数组中的元素必须符合Hashable协议.现在我可以这样做:

 var dupes = ["one", "two", "two", "three"]
 let deDuped = dupes.removeDuplicates()
 dupes.removeDuplicatesInPlace()
 // result: ["one", "two", "three"]
Run Code Online (Sandbox Code Playgroud)