我想做这样的事情,但无法获得正确的语法或在网络上找到任何提供正确编写方式的地方:
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
.
这可能吗?如果是这样,语法是什么?