I'd like to do something like this, but can't get the syntax right or find anywhere on the web that gives the right way to write it:
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
}
}
So I want to conform Array
to my protocol JSONDecodeable
only when the elements of this array conform to JSONCollectionElement
.
Is this possible? If so, what's the syntax?
This isn't possible yet in Swift. You can see the same thing happen in the standard library: Array
doesn't gain Equatable
conformance when it's declared with Equatable
elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With