Implementing a multimap in Swift with Arrays and Dictionaries

Posted by stuffy on Stack Overflow See other posts from Stack Overflow or by stuffy
Published on 2014-06-13T03:23:08Z Indexed on 2014/06/13 3:24 UTC
Read the original article Hit count: 267

Filed under:

I'm trying to implement a basic multimap in Swift. Here's a relevant (non-functioning) snippet:

class Multimap<K: Hashable, V> {
    var _dict = Dictionary<K, V[]>()

    func put(key: K, value: V) {
        if let existingValues = self._dict[key] {
            existingValues += value
        } else {
            self._dict[key] = [value]
        }
    }
}

However, I'm getting an error on the existingValues += value line:

Could not find an overload for '+=' that accepts the supplied arguments

This seems to imply that the value type T[] is defined as an immutable array, but I can't find any way to explicitly declare it as mutable. Is this possible in Swift?

© Stack Overflow or respective owner

Related posts about swift