前⼀个字符串是颜⾊色的名称

OC中使用字典管理颜⾊,oc字典管理颜

  处理⼯程⽂件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 #EED9C4
Antique Brass #C88A65
Apricot #FDD5B1
Aquamarine #71D9E2
Asparagus #7BA05B
Atomic Tangerine #FF9966
Banana Mania #FBE7B2
Beaver #926F5B
Bittersweet #FE6F5E
Black #000000
Blizzard Blue #A3E3ED
Blue #0066FF
Blue Bell #9999CC
Blue Green #0095B6
Blue Violet #6456B7
Brick Red #C62D42
Brink Pink #FB607F
Brown #AF593E
Burnt Orange #FF7034
Burnt Sienna #E97451
Cadet Blue #A9B2C3
Canary #FFFF99
Caribbean Green #00CC99
Carnation Pink #FFA6C9
Cerise #DA3287
Cerulean #02A4D3
Chartreuse #FF9966
Chestnut #B94E48
Copper #DA8A67
Cornflower #93CCEA
Cotton Candy #FFB7D5
Cranberry #DB5079
Dandelion #FED85D
Denim #1560BD
Desert Sand #EDC9AF
Eggplant #614051
Electric Lime #CCFF00
Fern #63B76C
Flesh #FFCBA4
Forest Green #5FA777
Fuchsia #C154C1
Fuzzy Wuzzy Brown #C45655
Gold #E6BE8A
Goldenrod #FCD667
Granny Smith Apple #9DE093
Gray #8B8680
Green #01A368
Green Yellow #F1E788
Happy Ever After #6CDA37
Hot Magenta #FF00CC
Inch Worm #B0E313
Indian Red #B94E48
Indigo #4F69C6
Jazzberry Jam #A50B5E
Jungle Green #29AB87
Laser Lemon #FFFF66
Lavender #FBAED2
Macaroni And Cheese #FFB97B
Magenta #F653A6
Magic Mint #AAF0D1
Mahogany #CA3435
Manatee #8D90A1
Mango Tango #E77200
Maroon #C32148
Mauvelous #F091A9
Melon #FEBAAD
Midnight Blue #003366
Mountain Meadow #1AB385
Mulberry #C54B8C
Navy Blue #0066CC
Neon Carrot #FF9933
Olive Green #B5B35C
Orange #FF681F
Orchid #E29CD2
Outer Space #2D383A
Outrageous Orange #FF6037
Pacific Blue #009DC4
Peach #FFCBA4
Periwinkle #C3CDE6
Pig Pink #FDD7E4
Pine Green #01796F
Pink Flamingo #FF66FF
Plum #843179
Prussian Blue #003366
Purple Heart #652DC1
Purple Mountain's Majesty #9678B6
Purple Pizzazz #FF00CC
Radical Red #FF355E
Raw Sienna #D27D46
Razzle Dazzle Rose #FF33CC
Razzmatazz #E30B5C
Red #ED0A3F
Red Orange #FF3F34
Red Violet #BB3385
Robin's Egg Blue #00CCCC
Royal Purple #6B3FA0
Salmon #FF91A4
Scarlet #FD0E35
Screamin' Green #66FF66
Sea Green #93DFB8
Sepia #9E5B40
Shadow #837050
Shamrock #33CC99
Shocking Pink #FF6FFF
Silver #C9C0BB
Sky Blue #76D7EA
Spring Green #ECEBBD
Sunglow #FFCC33
Sunset Orange #FE4C40
Tan #FA9D5A
Tickle Me Pink #FC80A5
Timberwolf #D9D6CF
Torch Red #FD0E35
Tropical Rain Forest #00755E
Tumbleweed #DEA681
Turquoise Blue #6CDAE7
Ultra Green #66FF66
Ultra Orange #FF6037
Ultra Pink #FF6FFF
Ultra Red #FD5B78
Ultra Yellow #FFFF66
Unmellow Yellow #FFFF66
Violet (purple) #8359A3
Violet Red #F7468A
Vivid Tangerine #FF9980
Vivid Violet #803790
White #FFFFFF
Wild Blue Yonder #7A89B8
Wild Strawberry #FF3399
Wild Watermelon #FD5B78
Wisteria #C9A0DC
Yellow #FBE870
Yellow Green #C5E17A
Yellow Orange #FFAE42

 

处理⼯程⽂件crayons.txt中的⽂本信息,⽂本内容是关于颜色的,每⾏都是⼀个颜⾊的信息,例如:Almond
#EE…

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图