2

I want to extend dictionary and constrain it to specific key type of NSDate with values of type Array<MyClass>

MyClass is a swift class with no subclass.

extension Dictionary where Key: NSDate, Value: Array<MyClass>{
    func myFunction() -> Int{
        // Some thing useful
    }
}

The above code returns a build error:

Type 'Value' constrained to non-protocol type 'Array'

Ok so it seems that it needs to be constrained to a specific protocol, but strangely enough when I constrain Value to:

extension Dictionary where Key: NSDate, Value: MyClass

The compiler does not complain and the build succeeds. But MyClass is not a protocol. So what gives?

I've managed to constrain it like this:

extension Dictionary where Key: NSDate, Value: CollectionType, Value.Generator.Element: MyClass{
    func myFunction() -> Int{
        var count = 0
        for (_, graphicItemArray) in self{
            count+= (graphicItemArray as! [MyClass]).count
        }

        return count
    }
}

But this requires a forced cast because I need to get the count property in a Integer form.

So why can't I constrain it explicitly to value type of [MyClass] ?

the Reverend
  • 12,305
  • 10
  • 66
  • 121
  • 1
    Suggestion for your questions: The title could be something more specific to the problem, like *Extending dictionary with type constraint using a Struct*. Also if you remove `OP1ScheduleControlGraphicItem` from your examples and replace it with something like `NSString` or `UIView` (something that already exist) it makes it easier to copy and paste your examples and try them. – Maic López Sáenz Sep 28 '15 at 06:01
  • Yes thank you for pointing it out. I'll just put MyClass instead. I think its important to note that its a swift class without a subclass. – the Reverend Sep 28 '15 at 16:56

1 Answers1

1

Not being able to use a where clause with a struct may be an error. Checking the language reference for where clauses and following into

conformance-requirement → type-identifier­ | ...

Seems like type-identifier should accept a struct, even the example they use is Int.

This maybe be something to file a bug for.


Related to your specific problem, it seems to work if you use _ArrayType which is a protocol that Array implements and has the methods you need.

extension Dictionary where Key: NSDate, Value: _ArrayType ...

I am not sure tho if using that kind of internal protocols would flag your app and make it fail Apple review.

Maic López Sáenz
  • 10,385
  • 4
  • 44
  • 57
  • Using _ArrayType allows me to use the "count" property on the array. But as you say the question remains if this is legal. Also, I did not find a way to constrain it to a specific array of items of "MyClass". If no more solutions are posted I guess I'll take your advice and file a bug. Thank you! – the Reverend Sep 28 '15 at 16:59
  • I certainly hope it is a bug. Otherwise it is imposible to have dictionary extension constrained to `Value: String`, which seems rather silly. – Maic López Sáenz Sep 28 '15 at 21:25