私はこのようなことをしたいのですが、正しい構文を取得したり、それを書くための正しい方法を提供するWeb上の場所を見つけることができません:
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
}
}
したがって、この配列の要素が に準拠している場合にのみArray
、プロトコルに準拠したいと考えています。JSONDecodeable
JSONCollectionElement
これは可能ですか?もしそうなら、構文は何ですか?