通过唯一的key找到相应的value,类似于Map

NSDictionary是不可变的
 
1.创建
  1. void dicCreate()
  2. {
  3. //Immutable
  4. // NSDictionary *dic = [NSDictionary dictionary];
  5.  
  6. NSDictionary *dic = [NSDictionary dictionaryWithObject:@"Simon" forKey:@"name"];
  7.  
  8. dic = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];
  9.  
  10. NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
  11. NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
  12. dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
  13.  
  14. NSLog(@"%@", dic);
  15.  
  16. }
 
集中批量创建字典
  1. NSDictionary *d11_1 = @{@"姓名":@"张三", @"年龄":@"", @"性别":@"男"};
 
若存在同名的元素,采用最先定义的,多余的元素不会被计数
不允许nil作为键值
NSMutableDictioinary不能使用此方法,因为返回的是一个NSDictionary
 
 
2.基本操作
(1)键值对数量
(2)比较
(3)取值
(4)IO操作
  1. void dicUse()
  2. {
  3. NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
  4. NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
  5. NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
  6.  
  7. NSLog(@"%@", [dic objectForKey:@"k1"]);
  8. }
  9.  
  10. void dicUse2()
  11. {
  12. NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v1", @"v2", nil];
  13. NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
  14. NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
  15.  
  16. NSArray *keys2 = [dic allKeys];
  17. NSLog(@"%@", keys2);
  18.  
  19. NSArray *keys3 = [dic allKeysForObject:@"v1"];
  20. NSLog(@"%@", keys3);
  21.  
  22. NSArray *objs2 = [dic objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",@"k4",nil] notFoundMarker:@"not found"];
  23. NSLog(@"%@", objs2);
  24. }
 
3.遍历
  1. void dicLoop()
  2. {
  3. NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
  4. NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
  5. NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
  6.  
  7. //Loop all keys in dictionary
  8. for (id key in dic)
  9. {
  10. id value = [dic objectForKey:key];
  11. NSLog(@"%@ = %@", key, value);
  12. }
  13. }
 
4.迭代器
  1. void dicEnumerator()
  2. {
  3. NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
  4. NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
  5. NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
  6.  
  7. //key enumerator
  8. NSEnumerator *enumerator = [dic keyEnumerator];
  9. id key =nil;
  10. while (key = [enumerator nextObject])
  11. {
  12. id value = [dic objectForKey:key];
  13. NSLog(@"%@ = %@", key, value);
  14. }
  15. }
 
5.block迭代
  1. void dicBlockLoop()
  2. {
  3. NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
  4. NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
  5. NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
  6.  
  7. [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  8. NSLog(@"%@ - %@", key, obj);
  9. }];
  10. }
 
6.内存管理
  1. void memoryManage()
  2. {
  3. Student *stu1 = [Student studentWithName:@"Simon"];
  4. Student *stu2 = [Student studentWithName:@"Joke"];
  5. Student *stu3 = [Student studentWithName:@"Man"];
  6. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:stu1,@"stu1",
  7. stu2, @"stu2",
  8. stu3, @"stu3", nil];
  9.  
  10. //When dictionary is destroyed, the keys & objects will be released one time
  11. NSLog(@"stu1 before dic release:%zi", stu1.retainCount);
  12. }
 
7.取出键、值
(1)
  1. NSArray *da = [d allKeys];
(2)
  1. NSLog(@"%@", d[@""]);
 
8.NSDictionary是无序的,取出来的值不会按照存入的顺序排列
  1. NSMutableDictionary *d = [NSMutableDictionary dictionary];
  2. for (int i=; i<; i++)
  3. {
  4. [d setObject:@"abc" forKey:[NSString stringWithFormat:@"%d", i]];
  5. }
  6.  
  7. NSLog(@"%@", d);
 
out:
2014-11-19 02:36:25.850 03-NSArray[5171:303] {
    0 = abc;
    1 = abc;
    10 = abc;
    11 = abc;
...
 
 
 

[OC Foundation框架 - 10] NSDictionary的更多相关文章

  1. OC Foundation框架—集合

    Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...

  2. iOS - OC Foundation 框架

    前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...

  3. OC Foundation框架—结构体

    一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...

  4. OC Foundation框架—字符串

    一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...

  5. OC中Foundation框架之NSDictionary、NSMutableDictionary

    NSDictionary概述 NSDictionary的作用类似:通过一个key ,就能找到对应的value 同样 NSDictionary是不可变的,一旦初始化完毕,里面的内容就无法修改 NSDic ...

  6. OC — (Foundation框架-NSDate)

    NSDate:是OC中处理日期时间的一个类,可以用来表示时间 获取当前的时间 NSDate *d = [NSDate date]; 创建日期时间对象 NSLog输出是当前时间 格林时间 格式化显示时间 ...

  7. Foundation框架--字典( NSDictionary NSMutableDictionary )

    基础知识 1.字典不允许相同的key,但允许有相同的value. 2,字典是无序的,字典不能排序. 3.字典里的内容是成对存在的,不会出现单数. 4.快速创建的方式只适合不可变字典. 不可变字典 #i ...

  8. [OC Foundation框架 - 23] 文件管理

    A. 目录管理 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 // 1.获取文件属性 NSString *path = @& ...

  9. [OC Foundation框架 - 20] 统计代码行数

    注意: 1.变量名和函数名不要混淆调用 2.不要对文件夹进行文件的操作,没有权限 3.递归调用注意初始化变量   // // main.m // CodeLineCount // // Created ...

随机推荐

  1. C++不同进制整数

    在C++的整数常量中,整数分为十进制整数.八进制整数和十六进制整数. 那给出一个整型常量怎样区分是何种进制呢?/给出一个整型常量,如100,默认是十进制数,如果在该数前加0,如0100,则此数表示为八 ...

  2. 第二个C语言代码

    有问题,还没找出哪里出错了       输入一串字符,问号结束 统计1~9各出现的次数 ******************************************************** ...

  3. Stop-The-World

    Stop-The-World –Java中一种全局暂停的现象 –全局停顿,所有Java代码停止,native代码可以执行,但不能和JVM交互 –多半由于GC引起 •Dump线程 •死锁检查 •堆Dum ...

  4. *IntelliJ IDEA使用Hibernate连接数据库

    在IntelliJ IDEA中配置MySQL Database.

  5. POJ1573——Robot Motion

    Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...

  6. 利用CodeIgniter中的Email类发邮件

    CodeIgniter拥有功能强大的Email类.以下为利用其发送邮件的代码. 关于CI的Email类的详情请参考:http://codeigniter.org.cn/user_guide/libra ...

  7. Nandflash 驱动移植

    前段时间,研究了一下4G的Nandflash驱动.手头上只有飞凌6410BSP自带的Nandflash驱动,该驱动不支持K9GAG08U0D(2G)和K9LBG08U0D(4G)的Nandflash. ...

  8. Mac查看端口占用情况

    Mac下使用lsof(list open files)来查看端口占用情况,lsof 是一个列出当前系统打开文件的工具. 使用 lsof 会列举所有占用的端口列表: $ lsof 使用less可以用于分 ...

  9. 三维软件转Unity的系统单位设置研究

    Unity的系统单位为米,其他3D软件的模型导入,而保持和Unity的比例一致是非常重要的,下面对各软件进行测试: ㈠. 3dsmax 转 Unity的比例为100:1:也就是说Unity单位是3ds ...

  10. sql 的错误处理功能很弱

    --下面演示了SQL错误处理的脆弱性--邹建 --演示1--测试的存储过程1create proc p1asprint 12/0if @@error<>0print '发生错误1' sel ...