Objective C Classic Dictionary array sort Province
- 2020-10-07 18:53:18
- OfStack
1. First of all, prepare the document containing all the provinces and cities, and drag the document into the Xcode we have built
NSString *path = @" "; Then drag the provincial document to @" ";
The problem is how to load dictionaries into arrays and then arrays into dictionaries. So the key is how to nest each dictionary and array into its corresponding array and dictionary
>
The dictionary -
>
Array -
>
The dictionary -
>
An array of
4. After the above preparations are completed, we can start sorting the provinces and cities
Produces the corresponding character based on the path
tring *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
Cut the string into an array
NSArray *array = [str componentsSeparatedByString:@"\n"];
To establish 1 A large array of
NSMutableArray *mutableArray = [NSMutableArray array];
for (NSString *str in array) {
Determines non-space characters , Find out the province
if (![str hasPrefix:@" "]) {
For each 1 A province name , create 1 A dictionary
NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
Put province names in the dictionary , And set the Key for PROVIN
[proDic setObject:str forKeyedSubscript:@"PROVIN"];
create 1 An array , As a subordinate for the city
NSMutableArray *cityArray = [NSMutableArray array];
Store the city array in the dictionary , Set up the key position CITYARRAY
[proDic setObject:cityArray forKeyedSubscript:@"CITYARRAY"];
[mutableArray addObject:proDic];
}
Find the city name
if ([str hasPrefix:@" "]&& ![str hasPrefix:@" "]) {
Pull out the end of the array 1 An element , Represents the provincial dictionary to which the city belongs
NSDictionary *proDic = [mutableArray lastObject];
Pull out the array reserved in the dictionary
NSMutableArray *cityArray = [proDic objectForKey:@"CITYARRAY"];
For each 1 A city of , create 1 A dictionary
NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
Put the city name in the dictionary
[cityDic setObject:str forKey:@"CITY"];
Create an array for the subregion as a reserve
NSMutableArray *areaArray = [NSMutableArray array];
In the dictionary
[cityDic setObject:areaArray forKey:@"AREAARRAY"];
Store the city dictionary in an array
[cityArray addObject:cityDic];
}
Find out the subordinate area
if ([str hasPrefix:@" "]) {
Find the provincial dictionary to which you belong
NSDictionary *proDic = [mutableArray lastObject];
Find the market array
NSMutableArray *cityArray = [proDic objectForKey:@"CITYARRAY"];
Find the dictionary of the storage market
NSDictionary *cityDic = [cityArray lastObject];
Find the number area in the middle of the dictionary
NSMutableArray *areaArray = [cityDic objectForKey:@"AREAARRAY"];
Store the region name in an array
[areaArray addObject:str];
}
}
for (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-ES19en classic dictionary array sorting example code, thank you for your support for this site!