A classic provincial and municipal area topic Dictionary and array looping nesting is a great help for beginners to understand dictionaries and arrays.
Structure diagram:
#import <Foundation/Foundation.h>
int Main (int argc, const Char * argv[]) {
// first version
NSString * Path = @ "/users/bruce_lin/desktop/area.txt";
NSString * content = [NSString stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];
//
Nsarray * Filearr = [content componentsseparatedbystring:@ "\ n"];
//
Nsmutablearray *mainarr = [Nsmutablearray array];
//
For (NSString *one in Filearr)
// {
if (![ One hasprefix:@ ""])
// {
nsmutabledictionary * province = [nsmutabledictionary dictionary];
Nsmutablearray * Cityarr = [Nsmutablearray array];
//
[Province Setobject:cityarr Forkey:one]; add key-value keys for a province dictionary to the City Array for the province name value
[Mainarr addobject:province];// adds the province dictionary to the province array
// }
else if ([one hasprefix:@ ""]&&![ One hasprefix:@ ""])
// {
nsmutabledictionary * City = [Nsmutabledictionary dictionary];
Nsmutablearray * nextlevelcity = [Nsmutablearray array];
//
[City Setobject:nextlevelcity Forkey:one]; add a key value to the city dictionary the city name value is a sub-city array
NSString * a = [[[Mainarr] lastobject]allkeys]objectatindex:0];// takes the first element of the key array of the most recently added province dictionary ( City Name string )
[[[[Mainarr lastobject]objectforkey:a]addobject:city];// ] Add the city dictionary to the corresponding value ( City array ) /c17>
// }
else if ([One hasprefix:@ ""])
// {
fetch City Array The value of the most recently added City dictionary ( City array ) adds the subordinate city name string into the array
Nsmutablearray *temp = [[Mainarr Lastobject]objectforkey:[[[mainarr lastobject]allkeys]objectatindex:0]];
[[[Temp lastobject]objectforkey:[[[temp lastobject]allkeys]objectatindex:0]]addobject:one];
//
// }
// }
// second version // because the dictionary is out of order when printing the city name may be printed in the lower cities after the two codes are exchanged
nsstring *content = [nsstring stringwithcontentsoffile:@ "/users/bruce_lin/desktop/ Area.txt " encoding:nsutf8stringencoding error:nil]; //The contents of the file are transcoded and saved in a string
Nsarray * Mainarr = [content componentsseparatedbystring:@ "\ n"]; // split string with "\ n" in array
nsmutablearray *procvinarr = [nsmutablearray array]; // Build Province array to save the province dictionary
for (nsstring *str in Mainarr) // traversal of processed files
{
If (![ Str hasprefix:@ ""]) // If the prefix has no space for the province name
{
nsmutabledictionary * prodic = [nsmutabledictionary dictionary];
Nsmutablearray * Cityarr = [nsmutablearray array];
[Prodic setobject: str forkey:@ "Pro"]; // province Dictionary first key value pair for pro-- province name
[Prodic setobject: Cityarr forkey:@ "City"]; // Next key-value pair is city--city array
[Procvinarr addobject:p Rodic]; // Add the province dictionary to the province array
}
Else if ([str hasprefix:@ "] &&![ Str hasprefix:@ ""]) // If the prefix is only 2 spaces without 4 spaces then the city name /c22>
{
nsmutabledictionary *citydic = [nsmutabledictionary dictionary];
Nsmutablearray * nextcity = [nsmutablearray array];
/// Ibid. establishing two key values for a city dictionary different order interchange print order
[Citydic setobject: nextcity forkey:@ "Nextcityname"];
[Citydic setobject: str forkey:@ "CityName"];
// According to the file structure Span class= "S6" > remove Most recently added province dictionary @ "City" key ( that is, the city array
Nsmutablearray * temp = [[Procvinarr lastobject] objectforkey:@ "City"];
[Temp AddObject: Citydic]; // Add a city dictionary to the city array
}
Else if ([str hasprefix:@ "]) // If there is a prefix of four spaces
{
Nsmutablearray * Temp1 = [[Procvinarr lastobject]objectforkey:@ "City"];
[[[Temp1 lastobject]objectforkey:@ "Nextcityname"]addobject: str];
///Ibid. add a string from a subordinate city to An array after you remove the list of subordinate cities in the city dictionary
}
}
// Save array by transcoding to json< span class= "s2" > data Save in string
nsstring * s = [[nsstring alloc]initwithdata: [nsjsonserialization Datawithjsonobject:p rocvinarr options:nsjsonwritingprettyprinted error: Nil] encoding:nsutf8stringencoding];
NSLog(@ "%@", s);
return 0;
}
Ios-oc-nsdictionary Nsarray-nest