OC Exercises--slicing string processing color values and names (knowledge points: dictionaries, enumerations, arrays, strings)

Source: Internet
Author: User

/* 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)

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.