1. First prepare the document containing all the provinces and cities, and drag the document into the Xcode we built.
2. Then create a new path nsstring *path = @ ""; Then drag the provincial urban documents to the @ "";
3. Before sorting, we first need to filter the idea: The problem with this answer is how to load the dictionary into an array, and then load the array into the dictionary, so the key point is how to provide a small idea by nesting each dictionary and array into their corresponding array and dictionary: array-> dictionary-> array- > Dictionary-> array
- First, we're going to build a provincial array of all the provincial cities.
- And then build a provincial dictionary (containing the province name and the city array)
- Create a city dictionary again (with the same name and District array)
- After we have set up the corresponding dictionaries and arrays, we can just put all the cities and districts into the corresponding arrays by nesting.
4. After the completion of the above preparations, we can start the provincial urban sorting
produces the corresponding character according to the path tring *str = [NSString stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];
The string is cut into an array nsarray *array = [str componentsseparatedbystring:@ "\ n"];
Establish a large array nsmutablearray *mutablearray = [Nsmutablearray array]; For (NSString *str in array) {to judge a non-whitespace character, find the province if (![
STR hasprefix:@ ""]) {Create a dictionary nsmutabledictionary *prodic = [Nsmutabledictionary dictionary] for every province name found;
Save the name in the dictionary and set key to Provin [Prodic setobject:str forkeyedsubscript:@ "Provin"];
Create an array to use as a nsmutablearray *cityarray = [Nsmutablearray array];
Store the city array in the dictionary, set the key bit Cityarray [prodic setobject:cityarray forkeyedsubscript:@ "Cityarray"];
[Mutablearray Addobject:prodic]; Locate the city name if ([str hasprefix:@ ""]&&![
STR hasprefix:@ ""] {take out the last element of the array, representing the provincial Dictionary of the city nsdictionary *prodic = [Mutablearray lastobject];
Remove the reserved array in the dictionary nsmutablearray *cityarray = [prodic objectforkey:@ "Cityarray"]; Every city you find, create a dictionary nsmutabledictiOnary *citydic = [Nsmutabledictionary dictionary];
Put the city name into the dictionary [citydic setobject:str forkey:@ "Town"];
Create a number group for the subordinate area as reserved nsmutablearray *areaarray = [Nsmutablearray array];
deposited in a dictionary [citydic setobject:areaarray forkey:@ "Areaarray"];
Deposit the city dictionary in an array [Cityarray addobject:citydic];
Locate the subordinate area if ([str hasprefix:@ ""]) {find the Subordinate province dictionary nsdictionary *prodic = [Mutablearray lastobject];
Find out the city array nsmutablearray *cityarray = [prodic objectforkey:@ "Cityarray"];
Find out the dictionary of the storage city nsdictionary *citydic = [Cityarray lastobject];
Find out the dictionary Central number district Nsmutablearray *areaarray = [citydic objectforkey:@ "Areaarray"];
Store the area name in an array [Areaarray ADDOBJECT:STR];
(Nsdictionary *dic in Mutablearray) {NSLog (@ "%@", [dic objectforkey:@ "Provin"]);
For (Nsdictionary *citydic in [dic objectforkey:@ "Cityarray"]) {NSLog (@ "%@", [Citydic objectforkey:@ "City"]); For (NSString *string in [citydic objectforkey:@ "Areaarray"]) {NSlog (@ "%@", string);
}}}</span></span>
Above is the Objective-c Classic dictionary array of sample code, thank you for your support for this site!