//
// main.m
// oc- 05 字典和集合
//
// Created by dllo on 15/10/29.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Color.h"
int main(int argc, const char * argv[]) {
// 不可变字典
//创建初始化 存储时无序的,其实存储是一种算法
//NSDictionary dictionaryWithObjectsAndKeys:value1,key1, value2, key2,....., nil
// NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"王宁", @"name", @"男", @"sex", @"瑜伽", @"hobby", nil];
// //输出 通过key访问value
// NSLog(@"%@", [dic objectForKey:@"name"]);
//
// for (NSInteger i = 0; i < dic.allKeys.count; i++) {
// //先通过下标获取key
// NSString *key = [dic.allKeys objectAtIndex:i];
// //再通过key获取value
// NSLog(@"%@", [dic objectForKey:[dic.allKeys objectAtIndex:i]]);
//
// }
//
// NSDictionary *str = [NSDictionary dictionaryWithObjectsAndKeys:@"大哥", @"name", @"男神", @"sex", @"英雄联盟", @"like", nil];
// for (NSInteger i = 0; i < str.allKeys.count; i++) {
// NSLog(@"%@", str.allKeys);
// NSLog(@"%@", [str objectForKey:[str.allKeys objectAtIndex:i]]);
// NSString *key = [str.allKeys objectAtIndex:i];
// NSLog(@"%@", [str objectForKey:key]);
// NSString *value = [str.allValues objectAtIndex:i];
// }
//可变字典 比较常用
//创建&初始化
// NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"王宁", @"name", @"男", @"sex", @"瑜伽", @"hobby", nil];
// NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
//添加修改
//若key值存在则修改, 不存在则添加
// [dic setObject:@"王宁宁" forKey:@"name"];
//删除
// [dic removeObjectForKey:@"sex"];
//集合
//不可变集合
//注意 1 集合注重的操作方式是判断是否包含,判断交集并集等
// 2 集合中的元素具有唯一性,若重复保留一个
// NSSet *set = [NSSet setWithObjects:@"1", @"2", @"3", @"2", nil];
//个数
// NSLog(@"%ld", set.count);
//获取任意一个元素
// NSLog(@"%@", [set anyObject]);
//判段是否包含一个元素
// NSLog(@"%d", [set containsObject:@"2"]);
//可变集合
// NSMutableSet *str =[NSMutableSet setWithObjects:@"1", @"2", @"3", nil];
// NSMutableSet *str2 =[ NSMutableSet setWithObjects:@"3", @"4", @"5", nil];
//合并
// [str unionSet:str2];
//添加
// [str addObject:@"9"];
//交集
// [str intersectSet:str2];
//删除
// [str removeObject:@"9"];
//可判断重复元素个数的集合
// NSCountedSet *str3 = [NSCountedSet setWithObjects:@"3", @"4", @"5", nil];
//元素个数
// NSLog(@"%ld", str3.count);
//重复的元素个数
// NSLog(@"%ld", [str3 countForObject:@"2"]);
//快速枚举
//数组
//遍历打印
// NSArray *arr = [NSArray arrayWithObjects:@"1", @"2", @"3" ,nil];
// for (NSInteger i = 0; i < arr.count; i++) {
// NSLog(@"%@",[arr objectAtIndex:i]);
// }
//
// for (NSString *c in arr) {
// NSLog(@"%@", c);
// }
//字典的快速枚举
// NSMutableDictionary *dic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"王宁", @"name", @"男", @"sex", @"瑜伽", @"hobby", nil];
//注意遍历字典得到的是key
// for (NSString *key in dic4) {
// NSLog(@"%@", [dic objectForKey:key]);
//
// }
//集合的快速枚举
// NSMutableSet *str5 =[NSMutableSet setWithObjects:@"1", @"2", @"3", nil];
// for (NSString *s in str5) {
// NSLog(@"%@", s);
// }
//注意!!! 快速枚举- 不要在 forin中改变被遍历的collection(换一句话说, 就是只能读,不能写改)
//可变数组的冒泡排序
// NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"4", @"3" ,@"5", @"9",nil];
// for (NSInteger i = 0; i < [arr3 count] - 1; i++) {
// for (NSInteger j = 0; j < [arr3 count] - 1 - i; j++) {
// if ([[arr3 objectAtIndex:j] compare: [arr3 objectAtIndex:j + 1]] > 0) {
// [arr3 exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
// }
// }
// }
// for (NSString *e in arr3) {
// NSLog(@"%@", e);
// }
//可变数组排序方法
// NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"4", @"3" ,@"5", @"9",nil];
// [arr3 sortUsingSelector:@selector(compare:)];
//
// for (NSString *e in arr3) {
// NSLog(@"%@", e);
// }
// 不可变数组排序方法
// NSArray *arr5 = [NSArray arrayWithObjects:@"4", @"3" ,@"5", @"9",nil];
// NSArray * stre = [arr5 sortedArrayUsingSelector:@selector(compare:)];
//
// for (NSString *e in stre) {
// NSLog(@"%@", e);
// }
//作业
NSString *filePath = @"/Users/dllo/Desktop/oc- 05 字典和集合/oc- 05 字典和集合/File";
NSString *str9 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//NSLog(@"%@", str9);
NSMutableArray *arr9 =[ NSMutableArray array];
NSMutableArray *key =[ NSMutableArray array];
NSMutableArray *value =[ NSMutableArray array];
NSMutableDictionary *color = [NSMutableDictionary dictionary];
NSArray *arr8 = [str9 componentsSeparatedByString:@" "];
//NSLog(@"%@", arr8);
for (NSInteger i = 0; i < arr8.count; i++) {
NSArray *arr7 = [[arr8 objectAtIndex:i] componentsSeparatedByString:@" #"];
[arr9 addObjectsFromArray:arr7];
}
// NSLog(@"%@",arr9);
for (NSInteger i = 0; i < arr9.count; i++) {
if (i % 2 == 0) {
[key addObject:[arr9 objectAtIndex:i]];
} else {
[value addObject:[arr9 objectAtIndex:i]];
}
}
// NSLog(@"%@",key);
//取出所得key值升序排列
[color setObject:value forKey:key];
// [color.allKeys sortedArrayUsingSelector:@selector(compare:)];
// // NSLog(@"%@", color.allKeys);
// NSLog(@"%@", arr2);
// NSLog(@"%@",arr1);
//取出所有的Value的值,按照排序后的key排列
[color.allValues sortedArrayUsingSelector:@selector(compare:)];
// for (NSString *b in color) {
// NSLog(@"%@", [color objectForKey:b]);
//
// }
//使用一个新的字典管理颜色,对颜色分类,"A", "B', "C", "D"...即这个字典包含多个键值对,key是26个字母,value是数组.数组里面存放的是color对象,需要自己创建color类.
// NSString *filePath = @"/Users/dllo/Desktop/oc- 05 字典和集合/oc- 05 字典和集合/File";
//文件地址
// NSString * str2 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//解析文件,接受文件内容
// NSArray *arr1 = [str2 componentsSeparatedByString:@" "];
//对文件进行每一行操作
//
//NSMutableDictionary *color2 =[[NSMutableDictionary alloc]init];
//初始化一个字典
// for (NSString *newstring in arr1) {
// NSArray *arr = [newstring componentsSeparatedByString:@" #"
// ];
//对每一行进行分割,返回数组
// [color2 setObject:arr[1] forKey:arr[0]];
// 将数组中的值取出来,添加到数据字典
// }
// NSArray *allkey = [color2 allKeys];
//get方法取得所有的key的值
// NSArray *newkey = [allkey sortedArrayUsingSelector:@selector(compare:)];
// NSLog(@"%@",newkey);
//取出所有的key值按照升序排列
// [[color2 allValues] sortedArrayUsingSelector:@selector(compare:)];
// for (NSString *b in color2) {
// NSLog(@"%@", [color2 objectForKey:b]);
// }
//4444
int a = 65;
NSMutableDictionary *newColor =[NSMutableDictionary dictionary];
for (NSInteger i = 0; i < 26 ; i++) {
//NSMutableArray *colorValueArr = [NSMutableArray array];
//Color *color1 = [[Color alloc]init];
//NSMutableArray *value = [NSMutableArray array];
NSMutableArray *key = [NSMutableArray array];
//[colorValueArr addObject:color];
[key addObject:[NSString stringWithFormat:@"%c", a++ ]];
[newColor setObject:value forKey:key];
}
// for (NSString *key in color) {
// NSString *keyHead = [key substringToIndex:1];
// NSMutableArray *colorValueArr = [NSMutableArray array];
// Color *color1 = [[Color alloc]initWithName: key colorValue:[color valueForKey:key]];
// [colorValueArr addObject:color1];
// [[newColor valueForKey:keyHead] addObject:color1];
//
// }
for (NSString *keys in color) {
// NSMutableArray *value = [NSMutableArray array];
NSString *str = [keys substringFromIndex:1];
NSLog(@"%@",str);
Color *p = [[Color alloc]init];
[p setColorName:keys];
[p setColorValue:[color valueForKey:keys]];
NSMutableArray *value = [newColor objectForKey:str];
if (value == nil) {
value = [NSMutableArray array];
[newColor setObject:value forKey:str];
}
[value addObject:p];
}
for (NSString *key in newColor) {
for (Color *color1 in [color valueForKey:key]) {
NSLog(@"key = %@,name = %@,value = %@", key, [color1 colorName], [color1 colorValue] );
}
}
return 0;
}