I have a great problem with a dictionary in a dictionary in Swift. I have some custom classes: Team, Competition and Statistics. Team and Competition conform to protocol Hashable and Equatable.
The dictionary looks like this:
[Team: [Competition: Statistics]].
Furthermore I have two "overall values": a team and a competition.
When I do the following:
println(dictionary[overallTeam]!)
It prints the memory address of the competition and the statistics such you would expect. But, when I do the following:
println(dictionary[overallTeam]![overallCompetition])
I get nil as output. I'm totally wondering about that since there is only 1 key in dictionary[overallTeam]! which has the same name as the overallCompetition. That means that the equatable sign == returns true and the hashValue is identical.
Please help, where would be the problem in this.
Here's the code:
In the init() method I do the following:
dictionary = [Team: [Competition: Statistics]]()
dictionary[overallTeam] = [Competition: Statistics]()
dictionary[overallTeam]![overallCompetition] = Statistics()
The code for printing is in a method called when launching the application:
println(overallCompetition.name) //"overallCompetition"
println(overallCompetition.hashValue) //some hashCode, e.g. 5
for item in dictionary[overallTeam]!.keys { //executed once
println(item.name) //"overallCompetition"
println(item.hashValue) //SAME hashCode, e.g. 5
println(dictionary[overallTeam]![item]!.value) //prints the value expected
}
println(dictionary[overallTeam]) //prints some memory addresses
println(dictionary[overallTeam]![overallCompetition]) //nil
println(dictionary[overallTeam]![overallCompetition]!.value) //error: unexpectedly found nil
Just a general question: Dictionary keys are based on a hashValue, aren't they? Then why do two keys with the same hashValue not produce the same result???