关于字典管理颜色,对颜色进行分类管理的问题

来源:互联网 发布:园林绿化施工网络 编辑:程序博客网 时间:2024/06/05 07:50

题目:

处理工程文件crayons.txt中的文本信息,文本内容是关于颜色的,每行都是一个颜色的信息,例如:Almond #EED9C4,前一个字符串是颜色的名称,后一个字符串是颜色的16进制色值,处理文本完成如下需求

        1、使用字典管理所有的颜色,即字典中存储的是多个键值对,颜色名称为key16进制颜色值(不带#)是value

        2、取出所有的key,升序排列。

        3、取出所有的value,按照排序后的key排列。

        4、使用一个新的字典管理颜色,对颜色进行分类管理,即:“A”“B”“C”…即这个字典包含多个键值对,key26个字母,value是数组,数组里存放的是Color对象(包含namecolorValue)。需要自己创建Color类。

        

       提示

        //获取文件路径

        //找到文件->右键(显示简介)->位置就是文件的绝对路径(一定记得加文件的名字)

        NSString *filePath = @"/Users/fuxiaohui/公司/上课代码/河南34班上课例子/OC/ColorManager/ColorManager/crayons.txt";

        //读取文件中得字符串

        NSString *originalString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];





Color.h:

@interface Color : NSObject


@property (nonatomic, retain) NSString *name;

@property (nonatomic, retain) NSString *colorValue;


@end


main.m

NSString *filePath = @"/Users/lanouhn/Desktop/这几天的作业/crayons.txt";//路径是根据自己文件存放的位置而变的

        

       //读取文件中得字符串

        NSString *originalString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

//这里可以打印一下字符串, 看看有没有获取到该字符串

        //NSLog(@"%@", originalString);


        //截取字符串, 使用componentsSeparatedByString方法

        NSArray *array = [originalString componentsSeparatedByString:@"\n"];

        //NSLog(@"%@", array);


        //创建一个字典

        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

//遍历整个数组, 使用快速枚举的方法

        for (id soft in array) {

//使用截取字符串的方法, 同上

            NSArray *array1 = [soft componentsSeparatedByString:@" #"];

            //array1看做一个二维数组

            [dic setObject:array1[1] forKey:array1[0]];

        }

        NSLog(@"%@", dic);

        

        //获取所有的key

        NSLog(@"----------获取所有的键值----------");

        NSArray *key = [dic allKeys];

        NSLog(@"%@", key);

        

        //取出所有的键值,升序排列

        //定义一个比较字符串的块方法, 便于下面进行调用

        NSComparator rule = ^(NSString *str1, NSString *str2) {

            NSComparisonResult result = [str1 compare:str2];

            return result;

        };

        

//调用上面定义的方法, 对数组softKey进行排序(升序)

        NSArray *softKey = [key sortedArrayUsingComparator:rule];

        NSLog(@"---------键值升序排列----------");

        NSLog(@"%@", softKey);

        

        //取出所有value,根据排好序的key

        NSMutableArray *softValue = [[NSMutableArray alloc] init];

//利用快速枚举遍历key值所在的数组

        for (id soft1 in softKey) {

            NSArray *value = [dic objectForKey:soft1];

            [softValue addObject:value];

        }

        NSLog(@"----取出所有value,根据排好序的key----");

        NSLog(@"%@", softValue);

        

        

        //4、使用一个新的字典管理颜色,对颜色进行分类管理,即:“A”“B”“C”…即这个字典包含多个键值对,key26个字母,value是数组,数组里存放的是Color对象(包含namecolorValue)。需要自己创建Color类。

        //新建一个字典

        NSMutableDictionary *newDic = [[NSMutableDictionary alloc] init];

//遍历字典

        for (id keys in dic) {

            //新建一个color对象,并初始化

            Color *color = [[Color alloc] init];

            //key值赋给color.name

            [color setName:keys];

            //value赋给colorValue

            [color setColorValue:[dic objectForKey:keys]];

            //找到color的首字母

            NSString *str = [color.name substringToIndex:1];

            //将首字母大写

            NSString *str1 = [str uppercaseString];

            //通过key的首字母值给新字典赋值

            NSMutableArray *value = [newDic objectForKey:str1];

            //进行判断, 看value是否为空

            if (value == nil) {

//为空的时候, 重新建一个数组, 存放value值

                value = [NSMutableArray array];

                //给新字典赋值

                [newDic setObject:value forKey:str1];

            }

//如果value不为nil, 说明数组存在, 直接往里面赋值

            [value addObject:color];

        }

        //打印新的字典内容

        NSLog(@"%@", newDic);



0 0
原创粉丝点击