"Learning iOS Path: objective-c" Array, string, dictionary sum exercises

Source: Internet
Author: User

①. Implementation method: 13 People, 3 people a group, with a large array to manage each grouping.

            1. Original array          Nsarray *array = @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13];                2. Number of original arrays        Nsinteger count = [array count];        3. Create a mutable array of 1.        Nsmutablearray *newarray = [[Nsmutablearray alloc] init];        4. Loop        for (Nsinteger i = 0;i < count; i++)        {                    //3, initialize a mutable array of 2            if (0 = = i% 3) {                Nsmutablearray *newarray1 = [[Nsmutablearray alloc] initwithcapacity:1];                Adds a variable array of 2 to a mutable 1.                [NewArray addobject:newarray1];                [NewArray1 release];               }        Gets the last element in the mutable array 1, which can be variable group 2.        Adding elements to a mutable array 2        [[NewArray Lastobject] addobject:array[i];        }              Output        NSLog (@ "%@", NewArray);        [NewArray release];        

②. processing? text information in the document Crayons.txt, the text content is about the color, every line is? A color of the information, such as: Almond #EED9C4, before? A string is the name of color, after? A string is the color of the color of the 16? To handle the text, complete the following requirements:
1, to use a dictionary to manage all the color, that is, the dictionary is stored in a number of key-value pairs, Yan-color name is key,16? Color value (without #) is value.
2. Remove all keys and arrange them in ascending order.
3. Take out all the value and arrange it according to the sorted key.
4, make? A new dictionary to manage the color, the color into the line classification management, namely: "A", "B", "C" ... That is, the dictionary contains multiple key-value pairs, key is 26 characters? The value is an array, and the array is a color object (containing name and ColorValue). You need to create a color class from yourself.
The implementation is as follows:

1. The code inCrayons.txt is as follows

Almond #EED9C4Antique Brass #C88A65Apricot #FDD5B1Aquamarine #71D9E2Asparagus #7BA05BAtomic Tangerine #FF9966Banana Mania #FBE7B2Beaver #926F5BBittersweet #FE6F5EBlack #000000Blizzard Blue #A3E3EDBlue #0066FFBlue Bell #9999CCBlue Green # 0095b6blue Violet #6456B7Brick Red #C62D42Brink Pink #FB607FBrown #AF593EBurnt Orange #FF7034Burnt Sienna #E97451Cadet Blu E #A9B2C3Canary #FFFF99Caribbean Green #00CC99Carnation Pink #FFA6C9Cerise #DA3287Cerulean #02A4D3Chartreuse # Ff9966chestnut #B94E48Copper #DA8A67Cornflower #93CCEACotton Candy #FFB7D5Cranberry #DB5079Dandelion #FED85DDenim # 1560BDDesert Sand #EDC9AFEggplant #614051Electric Lime #CCFF00Fern #63
The specific code is as follows:

     Get file path NSString *filepath = @ "/users/object-c daily job/crayons.txt";//file address//read string in file NSString *oldstring = [N    Sstring Stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil];    The file is first manipulated to remove the contents of each line.                Nsarray *array = [oldstring componentsseparatedbystring:@ "\ n"];    1, to use a dictionary to manage all the color, that is, the dictionary is stored in a number of key-value pairs, Yan-color name is key,16? Color value (without #) is value.    Initializes a dictionary nsmutabledictionary *dictionary = [[Nsmutabledictionary alloc] init]; Iterates through each row in the array for (NSString *newstring in array) {//splits each row, returns the array nsarray *arr = [NewString Co            mponentsseparatedbystring:@ "#";            The values in the array are taken out and added to the data dictionary.        [Dictionary setobject:arr[1] forkey:arr[0];               }//2, remove all keys in ascending order.        Nsarray *allkey = [dictionary AllKeys];  Nsarray *newkey = [Allkey sortedarrayusingcomparator:^ (ID obj1, id obj2) {return [(NSString *) obj1 Compare :(NSString *)OBJ2];               }];               NSLog (@ "--------%@", NewKey);        3. Remove all the value and arrange it according to the sorted key.        Remove all value values nsmutablearray *allvalue = [[Nsmutablearray alloc] init];            For (NSString *key in NewKey) {nsstring *value = [dictionary Objectforkey:key];        [Allvalue Addobject:value]; }//4. Use a new dictionary to manage the color, the color into the line classification management, namely: "A", "B", "C" ... That is, the dictionary contains multiple key-value pairs, key is 26 characters? The parent, value is an array, an array? The Color object (including name and ColorValue) is stored.              You need to create a color class.                1. Go through the dictionary first, take out the corresponding key and value values, and store them inside each color object.       Newly established dictionary nsmutabledictionary *newdictionary = [[Nsmutabledictionary alloc] init];        Nsmutablearray *newarray = [[Nsmutablearray alloc] init];            For (NSString *keys in dictionary) {//Create a Color object, add each key and value that is removed from the dictionary.            Color *color = [[Color alloc] init]; [Color Setcolorname:keys];             Add colorname [Color setcolorcode:[dictionary objectforkey:keys]];//Add ColorCode         Gets the first letter of the colorname of the Color object, nsstring *str = [[Color colorname] substringtoindex:1];            Case conversion NSString *string = [str uppercasestring];            NSLog (@ "s======string=%@", string); Determine if the dictionary exists key Nsmutablearray *value = [newdictionary objectforkey:string];//key color capital letters when key//nslog (            @ "s=======%@", value); Determine if there is an if (value = = nil) {//does not exist//build Array value = [Nsmutablearray                Array];                [NewArray Addobject:color];            [Newdictionary Setobject:value forkey:string];                                } [value Addobject:color];    } NSLog (@ "%@", newdictionary); }
The Color object properties are as follows:

@property (nonatomic, retain) nsstring *colorname; @property (nonatomic, retain) NSString *colorcode;


"Learning iOS Path: objective-c" Array, string, dictionary sum exercises

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.