この拡張機能を制約する正しい方法は何ですか?私の試練は成功しなかった。例えばSwiftを使用して辞書の配列を拡張する2.2
:
extension CollectionType where Generator.Element: [T.Key:Self.Value], T: DictionaryLiteralConvertible, T.Key: StringLiteralConvertible, T.Value: Encodable {
この拡張機能を制約する正しい方法は何ですか?私の試練は成功しなかった。例えばSwiftを使用して辞書の配列を拡張する2.2
:
extension CollectionType where Generator.Element: [T.Key:Self.Value], T: DictionaryLiteralConvertible, T.Key: StringLiteralConvertible, T.Value: Encodable {
Xcodeの8ベータ•スウィフト3
protocol Encodable {
var hashValue: Int { get }
}
extension Int: Encodable { }
extension Collection where Iterator.Element == [String: Encodable] {
var total: Int {
return reduce(0) {$0 + $1.values.reduce(0) {$0 + $1.hashValue}}
}
}
let dictionaries:[[String: Encodable]] = [["One":1],["Two":2],["Three":3, "Four":4]]
dictionaries.total // 10
のXcode 7.3.1•スウィフト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) {$0 + $1.values.reduce(0) {$0 + $1.hashValue}}
}
}
let dictionaries:[[String: Encodable]] = [["One":1],["Two":2],["Three":3, "Four":4]]
dictionaries.total // 10
_ArrayTypeにアンダースコアを使用する理由を明確にしてください。 – Laurent
http://swiftdoc.org/v2.2/protocol/_ArrayType/ –
http://swiftdoc.org/v2.2/protocol/_ArrayType/hierarchy/ –
これはかなり似ています:http://stackoverflow.com/questions/31956520/how-can-i-define-an-extension-to-collectiontype-so-that-its-methods-are-availabl。 –
あなたはOPを知っているだけなので、配列の関連タイプTはElementに名前が変更されました。 – BallpointBen