3

I would like to add an extension on Dictionary that only applies to a dictionary with Strings as Keys and Array of NSManagedObject as Value

Ideally it would look like this:

extension Dictionary where Key : StringLiteralConvertible, Value: [NSManagedObject] {

or

extension Dictionary where Key : StringLiteralConvertible, Value: SequenceType<NSManagedObject> {

If i only set Value : NSManagedObject, it works. But not if its an array of that. Does anyone know a workaround for this?

bogen
  • 9,954
  • 9
  • 50
  • 89
  • There is a discussion and a workaround in http://stackoverflow.com/questions/32815718/extending-dictionary-with-key-and-value-constraints – spassas Dec 16 '15 at 14:57

1 Answers1

0

EDIT:

Ok, this weirdly worked in the playground of Xcode 7.2...

extension Dictionary where Key: StringLiteralConvertible, Value: ArrayLiteralConvertible, Value.Element: NSManagedObject {
    func hello() -> String {
        return "World"
    }
}


let a: [String: [NSManagedObject]] = [:]
a.hello()

ORIGINAL:

I think you could try to specialize the Array type in Swift like this: Array<NSManagedObject>

So the result will look like this:

extension Dictionary where Key: StringLiteralConvertible, Value: Array<NSManagedObject> {
Lachezar
  • 6,523
  • 3
  • 33
  • 34
  • Unfortunately, this will also produce an error: `type 'Value' constrained to non-protocol type 'Array'` – spassas Dec 16 '15 at 14:58