iOS 开发 OC编程 字典和集合 排序方法
//
// 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:@"\n"];
//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:@"\n"];
//对文件进行每一行操作
//
//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;
}
转载于:https://www.cnblogs.com/yuhaojishuboke/p/5043121.html
iOS 开发 OC编程 字典和集合 排序方法相关推荐
- iOS 开发 OC编程 数组冒泡排序.图书管理
// // main.m // oc -5 数组 // // Created by dllo on 15/10/28. // Copyright (c) 2015年 dllo. All rig ...
- iOS 开发 OC编程 属性和字符串
// // main.m // oc 04属性&字符串 // // Created by dllo on 15/10/27. // Copyright (c) 2015年 dllo. ...
- ios开发oc高仿京东金融白条额度余额的 ios开发水波纹 ios开发水正弦曲线波纹 ios开发雷达扫描的动画效果
ios开发oc高仿京东金融白条额度余额的 ios开发水波纹 ios开发水正弦曲线波纹 直接上代码,复制粘贴就可以 vc里的 WaterRippleView *topView = [[Water ...
- ios开发oc高仿京东金融白条额度余额的 ios开发水波纹 ios开发水正弦曲线波纹 ios开发雷达扫描的动画效果...
ios开发oc高仿京东金融白条额度余额的 ios开发水波纹 ios开发水正弦曲线波纹 直接上代码,复制粘贴就可以 vc里的 WaterRippleView *topView = [[Water ...
- python字典value排序_python字典按照value排序方法
python中,我们可以对列表.字符串.元祖中的元素进行排序,那对于字典中的元素可以排序吗?其实对于字典本身我们无法进行排序,但是我们可以对字典按值排序.本文介绍python中对字典按照value进行 ...
- iOS开发屏幕旋转锁定横竖屏解决方法
iOS开发屏幕旋转锁定横竖屏解决方法 使用场景: 公司最近产品,有两个界面是横屏的,其他的界面是竖屏的.针对这个需求,也调试了一段时间.在网上也查找了不少资料. 解决的方案也是有的,但是都是需要在导航 ...
- ios加载本地html懒加载图片方案,IOS开发中加载大量网络图片优化方法
IOS开发中加载大量网络图片如何优化 1.概述 在IOS下通过URL读一张网络图片并不像其他编程语言那样可以直接把图片路径放到图片路径的位置就ok,而是需要我们通过一段类似流的方式去加载网络图片,接着 ...
- iOS开发-项目的完整重命名方法,图文教程。
前言:在IOS开发中,有时候想改一下项目的名字,都会遇到很多麻烦.直接改项目名吧,XCODE又不会帮你改所有的名字.总是有很多文件.文件夹或者是项目设置的项.而且都是不能随便改的,有时候改着改着,编译 ...
- java 集合排序方法_java集合排序方法sort的使用
转自 http://blog.csdn.net/a1165117473/article/details/6965652 /* * To change this template, choose To ...
最新文章
- Scrapy框架--使用cookie
- 深入思考编译原理之 理解执行过程和编译过程
- hdu 3790 最短路径dijkstra(多重权值)
- 天锐绿盾解密_天锐绿盾携手衡阳规划设计院 实现信息系统安全管理
- MyBatis Plus——分页插件
- Android深入浅出系列之Android工具的使用—调试桥ADB(二)
- How to resolve ATC error message Package Violation (Error) - Missing Use Access (USEM)
- open***2.3.12安装与easy-rsa3的使用
- scala基础之对象
- 与context的关系_Go中的Context超时和关闭是如何实现的呢?
- ssrs 生成pdf_在SSRS报告中生成热图的可用选项
- Exchange 2007 邮箱设置
- SVN客户端安装及使用
- SAP FB60\FB70\MIRO 默认税码配置
- 宏杉科技摆“擂台”,遍寻天下存储技术高手
- python输入个人所得税计算_python-计算个人所得税
- undolog实现事务原子性,redolog实现事务的持久性
- 笔记本电脑实现内外网双通成功经验分享(内网用有线,外网用无线)
- 基于Java实现的新冠肺炎疫情实时动态地图
- 哪一层提供了数据加密的功能?