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 type
The 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.
How are you testing this? I copied your code into a new test project (created from the OS X > Command Line Tool template) and it worked just fine.
k: 2, v: Green
k: 3, v: Blue
k: 1, v: Red
Program ended with exit code: 0
This is with Xcode 7.0 on OS X 10.10.5.
Share and Enjoy
—
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"