Ich möchte so etwas machen, kann aber die Syntax nicht richtig finden oder nirgendwo im Web finden, die den richtigen Weg zum Schreiben gibt:Array erweitern, um Protokoll zu entsprechen, wenn Element einem bestimmten Protokoll entspricht
protocol JSONDecodeable {
static func withJSON(json: NSDictionary) -> Self?
}
protocol JSONCollectionElement: JSONDecodeable {
static var key: String { get }
}
extension Array: JSONDecodeable where Element: JSONCollectionElement {
static func withJSON(json: NSDictionary) -> Array? {
var array: [Element]?
if let elementJSON = json[Element.key] as? [NSDictionary] {
array = [Element]()
for dict in elementJSON {
if let element = Element.withJSON(dict) {
array?.append(element)
}
}
}
return array
}
}
Deshalb mag ich Array
mein Protokoll JSONDecodeable
nur entsprechen, wenn die Elemente dieses Arrays zu JSONCollectionElement
entsprechen.
Ist das möglich? Wenn ja, wie lautet die Syntax?
Verwenden Generika so etwas wie Array –