I have a pretty complex data structure in my app, which I need to manipulate. I am trying to keep track of how many types of bugs a player has in thier garden. There are ten types of bugs, each with ten patterns, each pattern having ten colors. So there are 1000 unique bugs possible, and I want to track how many of each of these types the player has. The nested dictionary looks like:
var colorsDict: [String : Int] var patternsDict: [String : Any] // [String : colorsDict] var bugsDict: [String : Any] // [String : patternsDict]
I do not get any errors or complaints with this syntax.
When I want to increment the player's bug collection though, doing this:
bugs["ladybug"]["spotted"]["red"]++
I get this error: String is not convertible to 'DictionaryIndex< String, Any >' with the error's carrot under the first string.
Another similar post suggested using "as Any?" in the code, but the OP of that post only had a dictionary one deep so could do that easily with: dict["string"] as Any? ...
I am not sure how to do this with a multilevel dictionary. Any help would be appreciated.
A dictionary can contain any object type except another dictionary. Dictionaries are mutable. Dictionaries can be nested to any depth. Items are accessed by their position in a dictionary.
Without custom type in value of Dictionary, in Swift 2+ you can use the == operator to compare two Dictionary to check if they are equal or not. But in some cases with custom types as the Dictionary 's value (like struct ), you must adopt Equatable in order for that custom type to use == operator.
dictionary in Swift is used to store elements. Dictionary also contains one key while storing elements to it, later on, we can use these keys to access the elements store in the dictionary. Dictionary in swift does not maintain the insertion order of the element in which they are stored, they are unordered in nature.
When working with dictionaries you have to remember that a key might not exist in the dictionary. For this reason, dictionaries always return optionals. So each time you access the dictionary by key you have to unwrap at each level as follows:
bugsDict["ladybug"]!["spotted"]!["red"]!++
I presume you know about optionals, but just to be clear, use the exclamation mark if you are 100% sure the key exists in the dictionary, otherwise it's better to use the question mark:
bugsDict["ladybug"]?["spotted"]?["red"]?++
Addendum: This is the code I used for testing in playground:
var colorsDict = [String : Int]() var patternsDict = [String : [String : Int]] () var bugsDict = [String : [String : [String : Int]]] () colorsDict["red"] = 1 patternsDict["spotted"] = colorsDict bugsDict["ladybug"] = patternsDict bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 1 bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 2 bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 3 bugsDict["ladybug"]!["spotted"]!["red"]! // Prints 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With