使用 Swift 扩展字典数组 2.2

Extending an array of dictionaries using Swift 2.2

限制此扩展的正确方法是什么?我的试验没有成功。

例如:

extension CollectionType where Generator.Element: [T.Key:Self.Value], T: DictionaryLiteralConvertible, T.Key: StringLiteralConvertible, T.Value: Encodable {

Xcode 8 个测试版 • Swift 3

protocol Encodable {
    var hashValue: Int { get }
}
extension Int: Encodable { }

extension Collection where Iterator.Element == [String: Encodable] {
    var total: Int {
        return reduce(0) {[=10=] + .values.reduce(0) {[=10=] + .hashValue}}
    }
}

let dictionaries:[[String: Encodable]]  = [["One":1],["Two":2],["Three":3, "Four":4]]

dictionaries.total  // 10

Xcode 7.3.1 • Swift 2.2.1

protocol Encodable {
    var hashValue: Int { get }
}

extension Int: Encodable { }

extension CollectionType where Generator.Element == [String: Encodable] {
    var total: Int {
        return reduce(0) {[=11=] + .values.reduce(0) {[=11=] + .hashValue}}
    }
}

let dictionaries:[[String: Encodable]]  = [["One":1],["Two":2],["Three":3, "Four":4]]

dictionaries.total  // 10