Extension to constrained Dictionary Conforming to Protocol

拈花ヽ惹草 提交于 2019-12-11 10:09:42

问题


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.

来源:https://stackoverflow.com/questions/34950561/extension-to-constrained-dictionary-conforming-to-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!