[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 ...
随机推荐
- mybatis知识点
1.Mybatis比IBatis比较大的几个改进是什么 a.有接口绑定,包括注解绑定sql和xml绑定Sql , b.动态sql由原来的节点配置变成OGNL表达式, c. 在一对一,一对多的时候引进了 ...
- 使用Nginx+Keepalived组建高可用负载平衡Web server集群
一,首先说明一下网络拓扑结构: 1,Nginx 反向代理Server(HA): ①Nginx master:192.168.1.157 ②Nginx backup:192.168.1. ...
- 跨域使用jsonp 获取天气预报
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> ...
- R语言学习笔记:向量化
R语言最强大的方面之一就是函数的向量化,这些函数可以直接对向量的每个元素进行操作.例如: 对每个元素进行开方 > v<-c(4,3,8,16,7.3) > v [1] 4.0 3 ...
- linux netcat命令
netcat是网络工具中的“瑞士军刀”,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所 ...
- VMware下ubuntu与win8共享文件时/mnt/hgfs目录为空的解决办法
VMware下ubuntu(guest)与win8共享文件时/mnt/hgfs目录为空的解决办法 环境:VMware-player-5.0.2-1031769 + ubuntu13.04 1.安装vm ...
- python学习笔记四--元组
一.元组: 1. 不可变更的列表 2. 从语法上,她们是编写在小括号里,不是方括号里,列表是编写在方括号里的 3. 圆括号也同时用于表达式,如果想说明这是一个元组,不是表达式,可以在value后,关闭 ...
- Android开发UI之ListView中的Button点击设置
在ListView的Item中,如果有Button控件,那么要实现Button和Item点击都有响应,可以将Item的Layout中Button的focusable属性设为false,然后设置layo ...
- Emmet快速开发
标签元素关系展开 div.wrap>div.content>(div.inner_l+div.inner_r)^div.sider ------缩写展开如下---------------- ...
- SGU185 - Two Shortest
原题地址:http://acm.sgu.ru/problem.php?contest=0&problem=185 题目大意:给出一个无向图,求出从 1 到 n 的两条没有相同边的最短路径(允许 ...