I am trying to create a protocol that allows any object to be instantiated using JSON NSData.
I am trying to create an extension to [String: String] dictionary that conforms to this protocol. Unfortunately for some reason the following code doesn't work:
public protocol InitializableWithData {
init(data: NSData?) throws
}
extension Dictionary: InitializableWithData where Key: String, Value: String {
public init(data: NSData?) {
self.init()
// Parse NSData into a [String: String]
}
}
I get the following error:
Extension of type 'Dictionary' with constraints cannot have an inheritance clause
I have also tried with:
extension Dictionary: InitializableWithData where Key: NSString, Value: NSString {
public init(data: NSData?) {
self.init()
// Parse NSData into a [String: String]
}
}
Given that String is a struct, but still it doesn't work.