*题目要求: 处理⼯工程⽂文件crayons.txt中的⽂文本信息,⽂文本内容是关于颜⾊色的,每⾏行都是⼀一 个颜⾊色的信息,例如:Almond #EED9C4,前⼀一个字符串是颜⾊色的名称,后⼀一 个字符串是颜⾊色的16进制⾊色值,处理⽂文本完成如下需求*/
// (1) *************************************************************************
// 使⽤用字典管理所有的颜⾊色,即字典中存储的是多个键值对,颜⾊色名称为 key,16进制颜⾊色值(不带#)是value___________________________________________________________________
//读取文件路径,定义字符串接收地址
NSString *path = @"/Users/dlios/Desktop/Objective-C/ColorManage/ColorManage/crayons.txt";
//定义一个字符串通过地址来接收文件内容
NSString *sourceStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//用换行来分割字符串存储到数组arr中
NSArray *arr = [sourceStr componentsSeparatedByString:@"\n"];
//打印数组,检验对错
NSLog(@"%@", arr);
//定义一个数组arr1来存储数组arr用#号分割后的小字符串
NSMutableArray *arr1 = [NSMutableArray array];
//循环用#对数组arr中每个小字符串进行分割
for (int i = 0; i < [arr count]; i++) {
//定义一个数组来存储对每次对arr进行分割操作后截取的两段小字符串
NSArray *arr2 = [arr[i] componentsSeparatedByString:@" #"];
//将每次得到的小数组arr2依次添加到大数组arr1中
[arr1 addObjectsFromArray:arr2];
}
//打印arr1
NSLog(@"%@", arr1);
// (2) *************************************************************************
// 取出所有的key,升序排列__________________________________________________________
//定义可变数组array1用来存储后面字典的key
NSMutableArray *array1 = [NSMutableArray array];
//定义可变数组array1用来存储后面字典的value
NSMutableArray *array2 = [NSMutableArray array];
//循环 偶数为颜色的名字是key 奇数为颜色的编码是value
for (int i = 0; i < [arr1 count]; i++) {
if ((i + 2) % 2 == 0) {
[array1 addObject:arr1[i]];
}else{
[array2 addObject:arr1[i]];
}
}
//创建字典 存储对应的key-value key为颜色名 value为颜色编码
NSMutableDictionary *colorDic = [NSMutableDictionarydictionaryWithObjects:array2 forKeys:array1];
//打印字典
NSLog(@"%@", colorDic);
//取出字典中的key
NSArray *arrKey = [colorDic allKeys];
//对字典中的key排序 *用方法选择器选择排序方法
NSArray *arr4 = [arrKey sortedArrayUsingSelector:@selector(compare:)];
//打印排序后的数组
NSLog(@"%@", arr4);
// (3) *************************************************************************
// 取出所有的value,按照排序后的key排列______________________________________________
//定义数组存储所有的value
NSMutableArray *arr5 = [NSMutableArray array];
//取出所有value
for (NSString *key in arr4) {
//定义字符串 按照arr4中排序号的key把每次循环得出的value赋值到字符串qqq上
NSString *qqq = [colorDic objectForKey:key];
//把得出的value添加到数组arr5中
[arr5 addObject:qqq];
}
//按照数组arr4排序好的key打印存有所有value的数组arr5
NSLog(@"%@", arr5);
// (4) *************************************************************************
// 使⽤用⼀一个新的字典管理颜⾊色,对颜⾊色进⾏行分类管理,即:“A”,“B”,“C”... 即这个字典包含多个键值对,key是26个字⺟母,value是数组,数组⾥里存放的是 Color对象(包含name和colorValue)。需要⾃自⼰己创建Color类-------------------------------------------
//初始化manage类中的一个对象
Manage *colorDictionary = [[Manage alloc] init];
//定义一个manage中类型的可变字典存储最后大写字母和分组后的颜色组成的大字典
NSMutableDictionary *dic = [colorDictionary colorDictionary];
//打印字典 检验是否字典在类中是否定义正确
NSLog(@"%@", dic);
for (NSString *key in colorDic) {
//定义一个Color类的对象, 此对象为字典类型,通过在管理类中写好的字典的方法,通过字典的key(大写字母)循环依次给color赋值
Color *color = [[Color alloc] initWithName:key colorValue:[colorDic objectForKey:key]];
//通过color的key的首字母找出字典中应该存入的数组,"="后面为通过对象color的name的首字母找到大字典中对应的数组
NSMutableArray *arrayM1 = [dic valueForKey:[color.namesubstringToIndex:1]];
//给通过判断选择后的数组依次添加与分组名key(大写字母)相对应的对象
[arrayM1 addObject:color];
}
//打印最后结果,分组之后的字典
NSLog(@"%@", dic);
return 0;
}
Color类:
#import
@interface Color : NSObject
{
NSString *_name;
NSString *_colorValue;
}
- (id)initWithName:(NSString *)name colorValue:(NSString *)colorValue;
- (void)setName:()name;
- (NSString *)name;
- (void)setColorValue:()colorValue;
- (NSString *)colorValue;
@end
Color实现:
#import "Color.h"
@implementation Color
- (id)initWithName:(NSString *)name colorValue:(NSString *)colorValue
{
self = [super init];
if (self != nil) {
_name = name;
_colorValue = colorValue;
}
return self;
}
- (void)setName:()name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (void)setColorValue:()colorValue
{
_colorValue = colorValue;
}
- (NSString *)colorValue
{
return _colorValue;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"name:%@ colorValue:%@", _name, _colorValue];
}
@end
Manage类:
#import
@interface Manage : NSObject
{
NSMutableDictionary *colorDictionary;
}
- (NSMutableDictionary *)colorDictionary;
@end
Manage实现:
//
#import "Manage.h"
@implementation Manage
- (id)init
{
self = [super init];
if (self != nil) {
colorDictionary = [[NSMutableDictionary alloc] init];
[self groupDic];
}
return self;
}
- (void)groupDic
{
NSArray *groupNames = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E",@"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S",@"T", @"U", @"V", @"W", @"X", @"Y", @"Z",nil];
for (NSString *group in groupNames) {
NSMutableArray *arr = [NSMutableArray array];
[colorDictionary setObject:arr forKey:group];
}
}
- (NSMutableDictionary *)colorDictionary
{
return colorDictionary;
}
@end
转自:http://blog.csdn.net/qq_31810357