[OC Foundation框架 - 10] NSDictionary
通过唯一的key找到相应的value,类似于Map
void dicCreate()
{
//Immutable
// NSDictionary *dic = [NSDictionary dictionary]; NSDictionary *dic = [NSDictionary dictionaryWithObject:@"Simon" forKey:@"name"]; dic = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil]; NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; NSLog(@"%@", dic); }
NSDictionary *d11_1 = @{@"姓名":@"张三", @"年龄":@"", @"性别":@"男"};
void dicUse()
{
NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; NSLog(@"%@", [dic objectForKey:@"k1"]);
} void dicUse2()
{
NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v1", @"v2", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; NSArray *keys2 = [dic allKeys];
NSLog(@"%@", keys2); NSArray *keys3 = [dic allKeysForObject:@"v1"];
NSLog(@"%@", keys3); NSArray *objs2 = [dic objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",@"k4",nil] notFoundMarker:@"not found"];
NSLog(@"%@", objs2);
}
void dicLoop()
{
NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; //Loop all keys in dictionary
for (id key in dic)
{
id value = [dic objectForKey:key];
NSLog(@"%@ = %@", key, value);
}
}
void dicEnumerator()
{
NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; //key enumerator
NSEnumerator *enumerator = [dic keyEnumerator];
id key =nil;
while (key = [enumerator nextObject])
{
id value = [dic objectForKey:key];
NSLog(@"%@ = %@", key, value);
}
}
void dicBlockLoop()
{
NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ - %@", key, obj);
}];
}
void memoryManage()
{
Student *stu1 = [Student studentWithName:@"Simon"];
Student *stu2 = [Student studentWithName:@"Joke"];
Student *stu3 = [Student studentWithName:@"Man"];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:stu1,@"stu1",
stu2, @"stu2",
stu3, @"stu3", nil]; //When dictionary is destroyed, the keys & objects will be released one time
NSLog(@"stu1 before dic release:%zi", stu1.retainCount);
}
NSArray *da = [d allKeys];
NSLog(@"%@", d[@""]);
NSMutableDictionary *d = [NSMutableDictionary dictionary];
for (int i=; i<; i++)
{
[d setObject:@"abc" forKey:[NSString stringWithFormat:@"%d", i]];
} NSLog(@"%@", d);
0 = abc;
1 = abc;
10 = abc;
11 = abc;
[OC Foundation框架 - 10] NSDictionary的更多相关文章
- OC Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...
- iOS - OC Foundation 框架
前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...
- OC Foundation框架—结构体
一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...
- OC Foundation框架—字符串
一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...
- OC中Foundation框架之NSDictionary、NSMutableDictionary
NSDictionary概述 NSDictionary的作用类似:通过一个key ,就能找到对应的value 同样 NSDictionary是不可变的,一旦初始化完毕,里面的内容就无法修改 NSDic ...
- OC — (Foundation框架-NSDate)
NSDate:是OC中处理日期时间的一个类,可以用来表示时间 获取当前的时间 NSDate *d = [NSDate date]; 创建日期时间对象 NSLog输出是当前时间 格林时间 格式化显示时间 ...
- Foundation框架--字典( NSDictionary NSMutableDictionary )
基础知识 1.字典不允许相同的key,但允许有相同的value. 2,字典是无序的,字典不能排序. 3.字典里的内容是成对存在的,不会出现单数. 4.快速创建的方式只适合不可变字典. 不可变字典 #i ...
- [OC Foundation框架 - 23] 文件管理
A. 目录管理 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 // 1.获取文件属性 NSString *path = @& ...
- [OC Foundation框架 - 20] 统计代码行数
注意: 1.变量名和函数名不要混淆调用 2.不要对文件夹进行文件的操作,没有权限 3.递归调用注意初始化变量 // // main.m // CodeLineCount // // Created ...
随机推荐
- VMware linux与windows文件共享
将要共享的文件做成一个iso文件,然后打开VMware
- 5.查找最小的k个元素(数组)
题目: 输入n个整数,输出其中最小的k个,例如输入1,2,3,4,5,6,7,8这8个数,则最小的4个是1,2,3,4(输出不要求有序) 解: 利用快速排序的partition,算导上求第k大数的思想 ...
- html--offsetLeft,Left,clientLeft的关键--动态获取计算元素位置关系
动态计算元素位置关系的时候,必备... http://www.cnblogs.com/panjun-Donet/articles/1294033.html
- 如何:在 Winform 动态启动、控制台命令行?
需求 winForm 程序输出类型为 windows 程序(不是命令行程序) 在运行时想输入一些信息编译开发调试,如何实现这一功能 解答: AllocConsole.FreeConsole ...
- Android sd卡读取数据库
先在 Manifest 里添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE&q ...
- UVA 10041 Vito's Family (中位数)
Problem C: Vito's family Background The world-known gangster Vito Deadstone is moving to New York ...
- NPOI的版本查看
从github上clone源代码 git clone https://github.com/tonyqus/npoi.git 下载的版本库中,有一个名为Release Notes.txt的文件,在这个 ...
- Number of Rectangles in a Grid
Project Euler 85: Investigating the number of rectangles in a rectangular grid Number of Rectangles ...
- poj12月其他题解(未完)
最近编程的时间比较少啊…… poj3253 就是个合并果子,各种优先队列即可(显然单调队列最优) poj3263 线段树统计每个点被覆盖了多少次即可,注意要去重 poj3625 最小生成树 poj36 ...
- bzoj2431:[HAOI2009]逆序对数列
单组数据比51nod的那道题还弱...而且连优化都不用了.. #include<cstdio> #include<cstring> #include<cctype> ...