Expanding to a restricted vocabulary conforming to the protocol

I am trying to create a protocol that allows any objects to be created using JSON NSData.

I am trying to create an extension for a dictionary [String: String] 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 am getting the following error:

Extension of type 'Dictionary' with constraints cannot have an inheritance clause

      

I've also tried:

extension Dictionary: InitializableWithData where Key: NSString, Value: NSString {
    public init(data: NSData?) {
        self.init()
        // Parse NSData into a [String: String]
    }
}

      

Considering String is a struct but still it doesn't work.

+1
generics ios extension-methods swift protocols


source to share


No one has answered this question yet

See similar questions:

3
Extension of the restricted vocabulary protocol

or similar:

1106
Create a generic method limiting T to Enum
638
Does C # have extension properties?
105
Using protocols in the form of array types and function parameters in swift
49
Swift - Protocol Extensions - Property Defaults
2
Restrict inherited bound type in protocol
2
How do I add protocol conformance to a generic type?
1
Can you have a protocol extension for a specific type of generics in Swift?
1
Limiting generic type extensions with inheritance
0
Use the original class variable in an extension that conforms to the protocol
0
Extending UIButton according to protocol only



All Articles
Loading...
X
Show
Funny
Dev
Pics