1
votes

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???

1
Could you please post the code that does the set-up and the printing of the dictionary variable. - lindon fox
Your dictionary defines the inner dictionary as having a Statistics key, but the inner dictionary is initialized with a PlayerStatistics key and then the value is initialized as a Statistics object... Is Statistics a subclass of PlayerStatistics or is PlayerStatistics a subclass of Statistics because your code is kind of implying that both are subclasses of each other. Change the value type of the inner dictionary to Statistics and see if that works. - ad121
Oh sorry, forgot to change it, changed the names so that they're not that long here. - borchero
did you find the solution ? - Sultan Ali

1 Answers

-1
votes

You cannot compare a string to a different object type and get consistent results, because the relation isn't reflexive. In other words, while you may have implemented Equatable to say your object == string, it is not the case that therefore string == object, which would break it.

Since you are indexing the array by string name then you are never going to find it with overallCompetition. Either store the second dictionary with overallCompetition as the values, or use the overallCompetition.name to extract the value.