I am downloading some information from a JSON feed about countries, states and their cities. The way I have my final data structure is as follows:
An NSArray where each element holds an NSDictionary. Each NSDictionary has a key of the name of the country, and a value of an NSDictionary corresponding to the states. Each of those NSDictionarys hold a key of the name of the state and a value of an array with a list of the names of the cities.
I want to display each country in a different section in a table view. To return the number of countries (aka. numbeber of sections), I can just do [countriesArray count].
However, to return the number of states and cities of each country, that seems impossible with my current structure. I can access [countriesArray objectAtIndex:index], but after that, how can I access the value of that dictionary (the key is the name of the country)?
Should I restructure my data structures? If so, what’s the best way to sort this kind of data?
You can have below data structure which may help.
A container NSArray which has NSDictionary as its object.
Each dictionary has fixed keys like: countryName and stateInfo.
The value of this keys will be: string and NSDictionary.
Each stateInfo dictionary should have fixed keys like: stateName, cities
the value of this keys will be: string and NSarray of cities.