/* Process The text information in the project file Crayons.txt, the text content is about the color, each line is a color information, for example: Almond #EED9C4, before? A string is the name of the color, the next string is the colour color of the 16 color value, processing? text to complete the following requirements:
1, using the dictionary to manage all the colors, 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. 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 letters, value is an array, and the array is a color object (containing name and ColorValue). You need to create a color class yourself.
*/
Main.m
int main (int argc, const char * argv[]) {
@autoreleasepool {
1 reading a string from a file
NSString *txtstr = [NSString stringwithcontentsoffile:@ "/users/lanou3g/desktop/zc4d5/crayons.txt" encoding: Nsutf8stringencoding Error:nil];
NSLog (@ "original sequence");
NSLog (@ "%@", txtstr);
2 cutting with "\ n"
NSLog (@ "======================================================");
Nsarray *txtstrs = [txtstr componentsseparatedbystring:@ "\ n"];
NSLog (@ "%@ by \ n", txtstrs);
3 Creating a color Dictionary
Nsmutabledictionary *colordic = [[Nsmutabledictionary alloc] init];
4 times the enumeration group
For (NSString *colorstr in txtstrs) {
"#" cutting a color string
Nsarray *arr = [Colorstr componentsseparatedbystring:@ "#"];
Creating a key-value object in a dictionary
[Colordic setvalue:arr[1] forkey:arr[0];
}
NSLog (@ "%@", colordic);
Get all the keys and sort them
Nsarray *sortedkeys = [[Colordic allkeys]sortedarrayusingselector: @selector (compare:)];
Get all of the value requirements in the same order as key
1. To create an empty array
Nsmutablearray *sortedvalues = [[Nsmutablearray alloc]init];
2 traversing the sorted key array
For (NSString *key in Sortedkeys) {
NSString *value = [Colordic Valueforkey:key];
Put in an array
[Sortedvalues Addobject:value];
}
NSLog (@ "Sortedkeys%@", Sortedkeys);
NSLog (@ "Sortedvalues%@", sortedvalues);
1 Creating a large dictionary that manages color objects
Nsmutabledictionary *newcolordic = [[Nsmutabledictionary alloc]init];
2 traversing the Color data dictionary
For (NSString *key in Colordic) {
Gets the corresponding value
NSString *value = [Colordic Valueforkey:key];
Encapsulation of objects
Color *color = [[Color Alloc]initwithname:key colorvalue:value];
Get first letter
NSString *first = [[Key substringtoindex:1]uppercasestring];
Get Color grouping
Nsmutablearray *colorarr = [Newcolordic Valueforkey:first];
if (Colorarr = = nil) {
Create a group if the corresponding color grouping does not exist
Colorarr = [[Nsmutablearray alloc]init];
Group colors into a large dictionary of colors
[Newcolordic Setvalue:colorarr Forkey:first];
}
[Colorarr Addobject:color];
}
NSLog (@ "%@", newcolordic);
}
return 0;
}
Color.m
#import "Color.h"
@implementation Color
-(ID) Initwithname: (NSString *) name ColorValue: (NSString *) colorvalue
{
self = [super init];
if (self) {
_name = name;
_colorvalue =colorvalue;
}
return self;
}
-(NSString *) description
{
return [NSString stringwithformat:@ "color:%@%@", _name,_colorvalue];
}
@end
OC Exercises--slicing string processing color values and names (knowledge points: dictionaries, enumerations, arrays, strings)