Generic parameter 'Element' could not be inferred when adding to Array

Viewed 4139

I have a dictionary of arrays:

var myDict : [String:[SomeObj]] = [:]

To populate it, I try to add a value to the array at the correct index. If the array does not exist, it fails and I make a new array at that index:

if myDict[key]?.append(val) == nil {
    myDict[key] = [val]
}

I think I should be able to shorten this to:

myDict[key]?.append(val) ?? myDict[key] = [val]

However, instead I get the error: Generic parameter 'Element' could not be inferred. Why?

2 Answers
Related