处理?程?件crayons.txt中的?本信息,?本内容是关于颜色的,每?都是?个颜?的信息,例如:Almond #EED9C4,前?个字符串是颜?色的名称,后?个字符串是颜色的16进制?值,处理?本完成如下需求:
1、使用字典管理所有的颜?,即字典中存储的是多个键值对,颜?名称为key,16进制颜?色值(不带#)是value。
2、取出所有的key,升序排列.
3、取出所有的value,按照排序后的key排列。
4、使?用?个新的字典管理颜色,对颜?进?分类管理,即:“A”,“B”,“C”... 即这个字典包含多个键值对,key是26个字?母,value是数组,数组?存放的是 Color对象(包含name和colorValue)。需要?己创建Color类。
首先,我们创建一个Color类,Color.h声明部分:
@interface Color : NSObject{ NSString * _name; NSString * _value;}- (instancetype)initWithName:(NSString *)name value:(NSString *)value;- (NSString *)getName;- (NSString *)getValue;@end
Color.m实现部分:
#import "Color.h"@implementation Color- (instancetype)initWithName:(NSString *)name value:(NSString *)value{ self = [super init]; if (self) { _name = name; _value = value; } return self;}- (NSString *)getName{ return _name;}- (NSString *)getValue{ return _value;}- (NSString *)description{ return [NSString stringWithFormat:@"COLOR --> %@ %@",_name,_value];}@end
我们在main函数中实现题目的相关要求:
//从文档中读取字符串 NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/liguoxiang/Desktop/Product/OC/Lesson/HomeWork-06/HomeWork-06/crayons.txt" encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",dataString); //将字符串以 \n 做分割,得到所有表示颜色的字符串,存储在dataArray中 NSArray * dataArray = [dataString componentsSeparatedByString:@"\n"]; //创建空的可变的字典,存储表示颜色的键值对 NSMutableDictionary * colorDic = [NSMutableDictionary dictionaryWithCapacity:[dataArray count]]; //快速枚举dataArray,得到表示颜色的字符串 for (NSString * colorString in dataArray) { //分割colorString,得到颜色的名字和色值 NSArray * colorComponents = [colorString componentsSeparatedByString:@" #"]; NSString * colorName = colorComponents[0]; NSString * colorValue = colorComponents[1]; //将颜色的名字作为key,色值作为value,存储在字典中 [colorDic setObject:colorValue forKey:colorName]; } //枚举结束后,输出管理颜色的字典 NSLog(@"color dic = %@",colorDic); //创建分组字典 NSMutableDictionary * colorGroupDic = [NSMutableDictionary dictionaryWithCapacity:30]; //创建数组拿出所有key值 NSArray * colorNameArray = [colorDic allKeys]; for (int i = 0; i < [colorNameArray count]; i ++) { //取出颜色的名称 NSString * name = colorNameArray[i]; //取出颜色对应的value值 NSString * value = [colorDic objectForKey:name]; //初始color对象 Color * c = [[Color alloc] initWithName:name value:value]; //groupName为颜色的首字母 NSString * groupName = [name substringToIndex:1]; //取出对应的分组名下的数组 NSMutableArray * oneGroup = [colorGroupDic objectForKey:groupName]; //如果输入为空 if (nil == oneGroup) { //就在数组中添加color对象 oneGroup = [NSMutableArray arrayWithObject:c]; //然后将数组和分组名添加到字典中 [colorGroupDic setObject:oneGroup forKey:groupName]; }else{ //数组不为空,说明分组存在,直接添加就可以了 [oneGroup addObject:c]; } } NSLog(@"colorGroupDic = %@",colorGroupDic);
关于题目中用到的颜色文档(crayons.txt),由于无法上传工程文件,我将其附在后面方便读者阅读,请读者自行粘贴创建text文档,
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 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 #E6BE8AGoldenrod #FCD667Granny Smith Apple #9DE093Gray #8B8680Green #01A368Green Yellow #F1E788Happy Ever After #6CDA37Hot Magenta #FF00CCInch Worm #B0E313Indian Red #B94E48Indigo #4F69C6Jazzberry Jam #A50B5EJungle Green #29AB87Laser Lemon #FFFF66Lavender #FBAED2Macaroni And Cheese #FFB97BMagenta #F653A6Magic Mint #AAF0D1Mahogany #CA3435Manatee #8D90A1Mango Tango #E77200Maroon #C32148Mauvelous #F091A9Melon #FEBAADMidnight Blue #003366Mountain Meadow #1AB385Mulberry #C54B8CNavy Blue #0066CCNeon Carrot #FF9933Olive Green #B5B35COrange #FF681FOrchid #E29CD2Outer 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 #DEA681Turquoise Blue #6CDAE7Ultra Green #66FF66Ultra Orange #FF6037Ultra Pink #FF6FFFUltra Red #FD5B78Ultra Yellow #FFFF66Unmellow Yellow #FFFF66Violet (purple) #8359A3Violet Red #F7468AVivid Tangerine #FF9980Vivid Violet #803790White #FFFFFFWild Blue Yonder #7A89B8Wild Strawberry #FF3399Wild Watermelon #FD5B78Wisteria #C9A0DCYellow #FBE870Yellow Green #C5E17AYellow Orange #FFAE42