【发布时间】:2015-10-19 16:22:47
【问题描述】:
我想做这样的事情,但无法获得正确的语法或在网络上找到任何提供正确编写方式的地方:
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
}
}
所以我想仅当此数组的元素符合JSONCollectionElement 时,才使Array 符合我的协议JSONDecodeable。
这可能吗?如果有,语法是什么?
【问题讨论】:
-
使用像 Array
这样的泛型
标签: ios swift protocols protocol-extension