Use dictionary management in OC?

Source: Internet
Author: User

To process the crayons.txt? This information is about color, each of which is the information of the Yan, for example: Almond #EED9C4, the former string is the name of the color color, after the string is the colors of 16 binary? Value, processing? This completes the following requirements:

1, using a dictionary to manage all the Yan, that is, the dictionary is stored in a number of key-value pairs, Yan? The name is key,16. Color values (without #) are 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. Use a new dictionary to manage the color, to the Yan? 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.

First, we create a Color class that Color.h the Declarations section:

@interface color:nsobject{    * _name;     * _value;} -(Instancetype) Initwithname: (NSString *) Name value: (NSString *) value; -(NSString *) getName; -(NSString *) GetValue; @end

color.m Implementation section:

#import "Color.h"@implementationColor-(Instancetype) Initwithname: (NSString *) Name value: (NSString *) value{ Self=[Super Init]; if(self) {_name=name; _value=value; }    returnSelf ;}-(NSString *) getname{return_name;}-(NSString *) getvalue{return_value;}-(NSString *) description{return[NSString stringWithFormat:@"COLOR-%@%@", _name,_value];}@end

We implement the relevant requirements of the topic in the main function:

//reading a string from a documentNSString * datastring = [NSString stringwithcontentsoffile:@"/users/liguoxiang/desktop/product/oc/lesson/homework-06/homework-06/crayons.txt"encoding:nsutf8stringencoding Error:nil]; NSLog (@"%@", datastring); //divide the string into \ n to get all the strings that represent the color, stored in the DataArrayNsarray * DataArray = [datastring componentsseparatedbystring:@"\ n"]; //creates an empty, mutable dictionary that stores key-value pairs that represent colorsNsmutabledictionary * Colordic =[Nsmutabledictionary Dictionarywithcapacity:[dataarray Count]]; //Fast enumeration of DataArray to get a string representing color     for(NSString * colorstringinchDataArray) {                //split colorstring to get the name and color value of the colorNsarray * colorcomponents = [colorstring componentsseparatedbystring:@" #"]; NSString* ColorName = colorcomponents[0]; NSString* ColorValue = colorcomponents[1]; //The name of the color as the key, the color value as value, stored in the dictionary[Colordic setobject:colorvalue forkey:colorname]; }    //after the enumeration is finished, the output manages the dictionary of ColorsNSLog (@"color dic =%@", Colordic); //Create a group dictionaryNsmutabledictionary * Colorgroupdic = [Nsmutabledictionary dictionarywithcapacity: -]; //create an array to take out all key valuesNsarray * Colornamearray =[Colordic AllKeys];  for(inti =0; i < [Colornamearray count]; i + +) {        //Remove the name of the colorNSString * name =Colornamearray[i]; //Remove the value corresponding to the colorNSString * value =[Colordic Objectforkey:name]; //Initial Color ObjectColor * C =[[Color alloc] Initwithname:name Value:value]; //GroupName as the first letter of the colorNSString * groupName = [name Substringtoindex:1]; //remove an array from the corresponding group nameNsmutablearray * Onegroup =[Colorgroupdic Objectforkey:groupname]; //if the input is empty        if(Nil = =Onegroup) {            //adds a color object to the arrayOnegroup =[Nsmutablearray arraywithobject:c]; //then add the array and the group name to the dictionary[Colorgroupdic Setobject:onegroup forkey:groupname]; }Else{            //array is not empty, the description group exists, directly add it can be[Onegroup Addobject:c]; }} NSLog (@"colorgroupdic =%@", colorgroupdic);

About the color document used in the topic (crayons.txt), because I can not upload the project file, I attach it to the reader for easy reading, please paste the user to create text document,

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 #FB 607FBrown #AF593EBurnt Orange #FF7034Burnt Sienna #E97451Cadet Blue #A9B2C3Canary #FFFF99Caribbean Green # 00cc99carnation Pink #FFA6C9Cerise #DA3287Cerulean #02A4D3Chartreuse #FF9966Chestnut #B94E48Copper #DA8A67Cornflower # 93CCEACotton Candy #FFB7D5Cranberry #DB5079Dandelion #FED85DDenim #1560BDDesert Sand #EDC9AFEggplant #614051Electric Lime #CCFF00Fern #63B76CFlesh #FFCBA4Forest Green #5FA777Fuchsia #C154C1Fuzzy wuzzy Brown #C45655Gold #E6BE8 Agoldenrod #FCD667Granny Smith Apple #9DE093Gray #8B8680Green #01A368Green Yellow #F1E788Happy Ever after #6CDA37Hot Magen Ta #FF00CCInch Worm #B0E313Indian Red #B94E48Indigo #4F69C6Jazzberry Jam #A50B5EJungle Green #29AB87Laser Lemon #FFFF66Lav Ender #FBAED2Macaroni and Cheese #FFB97BMagenta #F653A6Magic Mint #AAF0D1Mahogany #CA3435Manatee #8D90A1Mango Tango #E772 00Maroon #C32148Mauvelous #F091A9Melon #FEBAADMidnight Blue #003366Mountain Meadow #1AB385Mulberry #C54B8CNavy Blue #0066CCNeon carrot #FF9933Olive Green #B5B35COrange #FF681FOrchid #E 29cd2outer Space #2D383AOutrageous Orange #FF6037Pacific Blue #009DC4Peach #FFCBA4Periwinkle #C3CDE6Pig Pink #FDD7E4Pine Green #01796FPink Flamingo #FF66FFPlum #843179Prussian Blue #003366Purple Heart #652DC1Purple Mountain's Majesty #9678B6Purple pizzazz #FF00CCRadical Red #FF355ERaw Sienna #D27D46Razzle Dazzle Rose #FF33CCRazzmatazz #E30B5CRed #ED0A3FRed Orange #FF3F34Red Violet #BB3385Robin's Egg Blue #00CCCCRoyal Purple #6B3FA0Salmon #FF91A4Scarlet #FD0E35Screamin'Green #66FF66Sea Green #93DFB8Sepia #9E5B40Shadow #837050Shamrock #33CC99Shocking Pink #FF6FFFSilver #C9C0BBSky Blue #76D7EASpring Green #ECEBBDSunglow #FFCC33Sunset Orange # Fe4c40tan #FA9D5ATickle Me Pink #FC80A5Timberwolf #D9D6CFTorch Red #FD0E35Tropical Rain Forest #00755ETumbleweed #DEA681T Urquoise Blue #6CDAE7Ultra Green #66FF66Ultra Orange #FF6037Ultra Pink #FF6FFFUltra Red #FD5B78Ultra Yellow #FFFF66Unmello W Yellow #FFFF66Violet (purple) #8359A3Violet Red #F7468AVivid Tangerine #FF9980Vivid Violet #803790White #FFFFFFWild Blue Yonder #7A89B8Wild Strawberry #FF3399Wild Watermelon #FD5B78Wisteria #C9A0DCYellow # Fbe870yellow Green #C5E17AYellow Orange #FFAE42

Use dictionary management in OC?

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.