1

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.

Cosmin
  • 2,840
  • 5
  • 32
  • 50
  • As far as I can remember you cannot conditionally create an extension conforming to a protocol based on some generic constraints :/ – luk2302 Jan 22 '16 at 16:00

0 Answers0