字典颜色对应

来源:互联网 发布:怎么做好淘宝网店 编辑:程序博客网 时间:2024/06/06 01:40
#import <Foundation/Foundation.h>
#import "Color.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 1.读取文件内容,存储到字符串对象中
//        NSString *text = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Documents/homework/OC/homework_05_02/homework_05_02/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
        
       NSString *text = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Documents/homework/OC/homework_05_02/homework_05_02/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
        // 2.获取字符串中每一行信息,存到数组中
        
//        NSArray *lineInfoArray = [text componentsSeparatedByString:@"\n"];
        NSArray *lineArray = [text componentsSeparatedByString:@"\n"];
        
        // 3.创建可变字典,准备存取颜色的键值对
//        NSMutableDictionary *colorDict = [NSMutableDictionary dictionary];
        
        NSMutableDictionary *colorDict = [NSMutableDictionary dictionary];
        
        // 4.遍历数组中的每一个元素,分隔并保存到字典中
//        for (NSString *str in lineInfoArray) {
//            // 4.1将一行信息通过“ #”拆分到数组中
//            NSArray *arr = [str componentsSeparatedByString:@" #"];
//            // 4.2 取出第一个元素作为key,最后一个元素作为value,保存到字典中
//            [colorDict setObject:arr.lastObject forKey:arr.firstObject];
//        }
        
        for (NSString *str in lineArray) {
            NSArray *arr = [str componentsSeparatedByString:@" #"];
            [colorDict setObject:[arr lastObject] forKey:[arr firstObject]];
        }
        
        
        
        
        
        
        // 5.取出所有的key升序排列
//        NSArray *allKeys = colorDict.allKeys;
//        allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
        
        NSArray *allKeys = [colorDict allKeys];
        allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
        
        
        
        // 6.取出所有的value按照升序后的key排列
//        NSMutableArray *allValues = [NSMutableArray array];
//        // 快速遍历所有的key
//        for (NSString *key in allKeys) {
//            // 通过key获取值,并添加到可变数组中
//            [allValues addObject:colorDict[key]];
//        }
//        
        NSMutableArray *allValues = [NSMutableArray array];
        for (NSString *key in allKeys) {
            [allValues addObject:colorDict[key]];
        }
        
        
        
        
        
        
        
        
        
        
        
        // 7. 创建大字典
        NSMutableDictionary *objcDict = [NSMutableDictionary dictionary];
        
        // 7.1.添加键值
//        for (NSString *key in allKeys) {
//            NSString *firstCharacter = [key substringToIndex:1];
//            if (!objcDict[firstCharacter]) {
//                [objcDict setObject:[NSMutableArray array] forKey:firstCharacter];
//            }
//        }
        
        
        NSMutableDictionary *bigDict = [NSMutableDictionary dictionary];
        
        for (NSString *key in allKeys) {
            NSString *firstChar = [key substringToIndex:1];
            if (!bigDict [firstChar]) {
                [bigDict setObject:[NSMutableArray array] forKey:firstChar];
            }
        }
        
        
        
        
        
        
        
        
        
        
        
        // 8.将颜色封装为对象,并存入大字典中
        for (NSString *key in allKeys) {
            // 8.1获取值对应的value
            NSString *value = colorDict[key];
            // 8.2把key和value封装为对象
            Color *color = [[Color alloc] init];
            [color setName:key];
            [color setColorValue:value];
            // 8.3获取首字母
            NSString *firsCharacter = [key substringToIndex:1];
            // 8.4通过首字母,获取对应的可变数组
            NSMutableArray *mutableArray = objcDict[firsCharacter];
            // 8.5将对象添加到可变数组中
            [mutableArray addObject:color];
            NSLog(@"%@", mutableArray);
        }
        
//        for (NSString *key in allKeys) {
//            NSString *value = colorDict[key];
//            Color *c = [[Color alloc] init];
//            [c setName:key];
//            [c setColorValue:value];
//            NSString *f = [key substringToIndex:1];
//            NSMutableArray *mu = bigDict[f];
//            [mu addObject:c];
//        }
//        NSLog(@"%@", bigDict);
        








@interface Color : NSObject
{
    NSString *_name;
    NSString *_colorValue;
}
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setColorValue:(NSString *)colorValue;
- (NSString *)colorValue;
@end






#import "Color.h"

@implementation Color
- (void)setName:(NSString *)name
{
    _name = name;
}
- (NSString *)name
{
    return _name;
}
- (void)setColorValue:(NSString *)colorValue
{
    _colorValue = colorValue;
}
- (NSString *)colorValue
{
    return _colorValue;
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"%@    :    %@", _name, _colorValue];
}
@end


0 0
原创粉丝点击