Objective-c Classical Dictionary array sorting-_ios in provinces and cities

Source: Internet
Author: User
Tags reserved

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

    1. First, we're going to build a provincial array of all the provincial cities.
    2. And then build a provincial dictionary (containing the province name and the city array)
    3. Create a city dictionary again (with the same name and District array)
    4. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.