I'm trying to implement a dictionary wrapper that uses a custom value type like so:
import Foundation
enum Color {
case Red
case Green
case Blue
}
struct IntColorDict {
private var dict: [Int: Color]
init(_ dict: [Int: Color]) {
self.dict = dict
}
}
extension IntColorDict: SequenceType {
typealias Generator = IntColorDictGenerator
func generate() -> Generator {
return IntColorDictGenerator(self)
}
}
struct IntColorDictGenerator: GeneratorType {
typealias Element = (Int, Color)
var generator: DictionaryGenerator<Int, Color>
init(_ colorDict: IntColorDict) {
generator = colorDict.dict.generate()
}
mutating func next() -> Element? {
return generator.next()
}
}If I now do
let c: [Int: Color] = [1: .Red, 2: .Green, 3: .Blue]
for (k, v) in IntColorDict(c) {
print("k: \(k), v: \(v)")
}the compiler fails with the error
fatal error: value failed to bridge from Swift type to a Objective-C typeThe problem obviously is the Color type, since when I replace all occurrences of Color with another Int (i.e. var dict: [Int: Int]) everything works fine. How can I fix this?
Or maybe there is even a completely different solution? What I actually need is a dictionary with a certain value type that has some extended functionality compared to default Swift Dictionaries (but only for that specific value type!). I cannot solve this problem by extending the Dictionary definition as extensions are not supported for data types with "where" constraints, so the wrapper above seems like a natural solution.