字典颜色对应
来源:互联网 发布:怎么做好淘宝网店 编辑:程序博客网 时间: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);
#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
- 字典颜色对应
- android 颜色对应值
- 颜色对应表
- 颜色值对应表
- android对应颜色值
- 颜色透明度对应值
- Python 字典中一键对应多个值
- Python 字典中一键对应多个值
- 常用颜色对应RGB颜色图
- 0x 颜色对应代码
- color颜色对应的数字
- 0x 颜色对应代码 .
- RGB颜色及对应值
- android 颜色及其对应值
- 0x 颜色对应代码
- YUV值对应的颜色
- 根据十六进制返回对应颜色
- 根据十六进制返回对应颜色
- Delphi 调用打印机指令ZPL实例--并口
- LeetCode | Count Primes
- F5系统配置备份及恢复
- 4-4
- opencv的配置
- 字典颜色对应
- 极光推送的使用方法
- Linux 设备驱动 Edition 3
- 如何理解离散傅里叶变换(一)实数形式傅里叶变换
- maven创建servlet3.0的web工程
- apply方法 Scala学习笔记-面向对象篇
- iOS 如何配置新建Xocde工程,识别C++语言混编
- ural 1057
- 4-5