iOS-OC-NSDictionary和NSMutableDictionary用法大全详细说明
2016-03-21 11:45
483 查看
+ (void)NSDictionaryCreateMethod { NSDictionary *dict = @{@"one":@"1",@"two":@"2",@"three":@"3",@"four":@"4"}; NSLog(@"dict = %@",dict); //减方法 NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil]; NSLog(@"dict1 = %@",dict1); //对应的加方法 NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil]; NSLog(@"dict2 = %@",dict2); NSDictionary *dict3 = [[NSDictionary alloc] initWithDictionary:dict]; NSLog(@"dict3 = %@",dict3); NSDictionary *dict4 = [NSDictionary dictionaryWithDictionary:dict]; NSLog(@"dict4 = %@",dict4); NSArray *keys = @[@"one",@"two",@"three",@"four"]; NSArray *objs = @[@"1",@"2",@"3",dict1]; NSDictionary *dict5 = [[NSDictionary alloc] initWithObjects:objs forKeys:keys]; NSLog(@"dict5 = %@",dict5); NSDictionary *dict6 = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; NSLog(@"dict6 = %@",dict6); } + (void)NSDictionarySimpleMethod { NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil]; //字典中的键值数 NSLog(@"count = %lu",[dict count]); //通过key找到对应的obj 不能通过obj 找到key NSLog(@"%@",[dict objectForKey:@"three"]); //遍历字典 枚举 或 快速枚举 NSEnumerator *e1 = [dict keyEnumerator]; id obj = nil; while (obj = [e1 nextObject]) { NSLog(@"key:%@,value:%@",obj,[dict objectForKey:obj]); } NSEnumerator *e2 = [dict objectEnumerator]; while (obj = [e2 nextObject]) { NSLog(@"value:%@",obj); } //快速枚举 实际上是字典key的枚举 for(id objin dict) { NSLog(@"obj = %@",obj);//和 NSLog(@"dict = %@",dict); 打印的顺序是不一样的 } NSArray *keyArray = [dict allKeys]; NSArray *objArray = [dict allValues]; for(int i =0;i<[dict count];i++) { NSLog(@"key : %@,value : %@",[keyArray objectAtIndex:i],[objArray objectAtIndex:i]); } } + (void)NSMutableDictionaryCreateMethod { NSDictionary *dict = @{@"one":@"1",@"two":@"2",@"three":@"3",@"four":@"4"}; NSMutableDictionary *dict1 = [[NSMutableDictionary alloc]initWithDictionary:dict]; NSLog(@"dict1 = %@",dict1); //对应的加方法 NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict]; NSLog(@"dict2 = %@",dict2); //创建一个空的可变字典 NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init]; NSLog(@"dict3&count = %lu",[dict3 count]); //对应的加方法 NSMutableDictionary *dict4 = [NSMutableDictionary dictionary]; NSLog(@"dict4&count = %lu",[dict4 count]); //可变字典自己的初始化方式 NSMutableDictionary *dict5 = [[NSMutableDictionary alloc] initWithCapacity:10]; NSLog(@"dict5&count = %lu",[dict5 count]); NSMutableDictionary *dict6 = [NSMutableDictionary dictionaryWithCapacity:10]; NSLog(@"dict6&count = %lu",[dict6 count]); } + (void)NSMutableDictionarySimpleMethod { NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil]; //增加字典元素 或者修改 若果没有增加 若果有修改 覆盖 [dict setObject:@"5" forKey:@"five"]; NSLog(@"%@",dict); [dict setObject:@"6666" forKey:@"five"]; NSLog(@"%@",dict); //因为字典是无序的 没有 交换插入 追加 //删除 [dict removeObjectForKey:@"five"]; NSLog(@"%@",dict); [dict removeAllObjects]; NSLog(@"%@",dict);//变成空字典了 //重设字典 NSDictionary *dict1 = @{@"one":@"111",@"two":@"222",@"three":@"333",@"four":@"444"}; [dict setDictionary:dict1]; NSLog(@"%@",dict);//变成空字典了 }
相关文章推荐
- iOS-OC-NSString和NSMutableString用法大全
- iOS-OC-监听键盘的高度 隐藏和出现
- iOS系统相册的有关操作
- iOS-OC-语音播放之切换听筒和扬声器的方法解决方案(红外感应)
- 对iOS锁的一些研究
- ionic input can't run on ios
- iOS_SN_CoreData数据迁移
- iOS 学习日志 : 向服务器发送Json数据
- iOS沙盒与清除缓存
- IOS开发他山之石系列-安装Alcatraz
- 深拷贝与浅拷贝及页面跳转时赋值
- iOS开发之玩转字符串
- 项目篇 - 良好习惯
- iOS实现能接受格式化字符串的方法
- IOS开发之格式化日期时间
- iOS判断判断设备类型信息
- image的变形处理,采用局部拉伸
- iOS OC生成验证码
- iOS 7之后,使用CIFilter生成二维码图片
- iOS SVN 工具和需要忽略的一些文件