OC3_字典
//
// main.m
// OC3_字典
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
//NSDictionary
//创建一个不可变字典对象
//NSMutableDictionary
//创建一个可变字典对象 //字典中对象都是键值对
//key:value
//字典中的键值对没有顺序
//字典中的key是唯一的, 但是value不一定是唯一的 int main(int argc, const char * argv[]) {
@autoreleasepool {
//用value 及 key 构造字典对象
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"",@"one",@"",@"two",@"",@"three",@"",@"four",@"",@"five",@"",@"six", nil];
NSLog(@"dict = %@", dict);
//创建常量字典对象
NSDictionary *dict2 = @{@"one":@"",@"two":@""};
NSLog(@"dict2 = %@", dict2); //用传入的字典对象创建字典对象
NSDictionary *dict3 = [[NSDictionary alloc] initWithDictionary:dict];
NSLog(@"dict3 = %@", dict3); //用传入的value数组及key数组创建字典对象
NSDictionary *dict4 = [[NSDictionary alloc] initWithObjects:@[@"",@"",@"",@""] forKeys:@[@"three",@"four",@"five",@"six"]];
NSLog(@"dict4 = %@", dict4); //创建一个空的字典对象
NSDictionary *dict5 = [[NSDictionary alloc] init];
NSLog(@"dict5 = %@", dict5); //类方法创建字典对象
//跟initWithObjectsAndKeys:方法对应
NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:@"",@"one",@"",@"two",@"",@"three", nil];
NSLog(@"dict6 = %@", dict6);
//跟initWithObjects:forKeys:相对应
NSDictionary *dict7 = [NSDictionary dictionaryWithObjects:@[@"",@"",@"",@""] forKeys:@[@"three",@"four",@"five",@"six"]];
NSLog(@"dict7 = %@", dict7); //跟initWithDictionary:相对应
NSDictionary *dict8 = [NSDictionary dictionaryWithDictionary:dict7];
NSLog(@"dict8 = %@", dict8); //计算字典中键值对的个数
NSUInteger len = [dict count];
NSLog(@"len = %li", len); //获取key对应的值
id value = [dict objectForKey:@"two"];
NSLog(@"value = %@", value); //获取所有的key
NSArray *keys = [dict allKeys];
NSLog(@"keys = %@", keys); //获取值对应所有的key
NSArray *keys2 = [dict allKeysForObject:@""];
NSLog(@"keys2 = %@", keys2); //获取所有的值
NSArray *values = [dict allValues];
NSLog(@"values = %@", values); //判断两个字典是否相等
BOOL ret = [dict isEqualToDictionary:dict2];
NSLog(@"ret = %i", ret); //遍历字典 -- 实际上是遍历字典的key //枚举器法
NSEnumerator *keysArray = [dict keyEnumerator];
for (id item in keysArray) {
NSLog(@"%@:%@",item,[dict objectForKey:item]);
}
//快速枚举法
for (id key in dict) {
NSLog(@"%@:%@", key, [dict objectForKey:key]);
}
//只遍历字典中的值
NSEnumerator *valuesArray = [dict objectEnumerator];
for (id item in valuesArray) {
NSLog(@"vlaue = %@", item);
} //可变字典操作
//构造指定容量大小的字典对象
//+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
NSMutableDictionary *mulDict = [[NSMutableDictionary alloc] initWithCapacity:];
NSLog(@"mulDict = %@",mulDict); //删除指定的key对应的键值对
NSMutableDictionary *mulDict2 = [NSMutableDictionary dictionaryWithDictionary:@{@"one":@"",@"two":@"",@"three":@"",@"four":@""}];
[mulDict2 removeObjectForKey:@"two"];
NSLog(@"mulDict2 = %@", mulDict2);
//添加(或者修改)键值对
[mulDict2 setObject:@"" forKey:@"three"];
NSLog(@"mulDict2 = %@", mulDict2); //将传入字典中的键值对添加到字典中 相同的修改,不同的就添加
[mulDict2 addEntriesFromDictionary:dict3];
NSLog(@"mulDict2 = %@", mulDict2); //删除所有的键值对
[mulDict2 removeAllObjects];
NSLog(@"mulDict2 = %@", mulDict2); //删除key数组对应所有键值对
NSMutableDictionary *mulDict3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"",@"five",@"",@"six",@"",@"seven",@"",@"eight", nil];
[mulDict3 removeObjectsForKeys:@[@"five",@"eight",@"nine"]];
NSLog(@"mulDict3 = %@", mulDict3);
//重置字典对象
[mulDict3 setDictionary:@{@"hello":@"",@"world":@""}];
NSLog(@"mulDict3 = %@", mulDict3); }
return ;
}
OC3_字典的更多相关文章
- 【DG】Oracle_Data_Guard官方直译
[DG]Oracle Data Guard官方直译 1 Oracle Data Guard 介绍 Oracle Data Guard概念和管理10g版本2 Oracle Data Guard ...
- DVWA实验之Brute Force(暴力破解)- Low
DVWA实验之Brute Force-暴力破解- Low 这里开始DVWA的相关实验~ 有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2 ...
- Oracle错误览表
Oracle 错误总结及问题解决 ORA 本文转自:https://www.cnblogs.com/zhangwei595806165/p/4972016.html 作者@承影剑 ORA-0 ...
- Javacript实现字典结构
字典是一种用[键,值]形式存储元素的数据结构.也称作映射,ECMAScript6中,原生用Map实现了字典结构. 下面代码是尝试用JS的Object对象来模拟实现一个字典结构. <script& ...
- python 数据类型 ----字典
字典由一对key:value 组成的 python中常用且重量级的数据类型 1. key , keys, values 字典由一对key:value 组成的 python中常用且重量级的数据类型 1. ...
- 增强版字典DictionaryEx
代码 public class DictionaryEx<TKey, TValue> : IDictionary<TKey, TValue> { /// <summary ...
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
- python之最强王者(8)——字典(dictionary)
1.Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
随机推荐
- SOD-80 LL34 DL-35 (2.7~75V)贴片稳压二极管【worldsing 笔记
¨ Silicon Planar Zener Diodes ¨ In Mini-MELF case especially for automatic insertion. ¨ The Zener vo ...
- SQL Server Profiler参数说明
上图依次说明为: TextDate 依赖于跟踪中捕获的事件类的文本值: ApplicationName 创建 SQL Server 连接的客户端应用程序的名称.此列由该应用程序传递的值填充,而不是由所 ...
- C++学习笔记(二):基本数据类型
带符号整数: short至少16位: int至少与short—样长: long至少32位,且至少与int—样长: long long至少64位,且至少与long—样长: 无符号整数: unsigned ...
- 10个Visual Studio原生开发调试技巧
10个Visual Studio原生开发调试技巧(1) 2013-05-29 13:30 佚名 开源中国 我要评论(1) 字号:T | T 以下的列表中你可以看到写原生开发的调试技巧(接着以前的文章来 ...
- Fox-H 函数
1.定义 2.特例 3.在分数阶微分方程中的应用 4.如何画图
- 安卓Android控件ListView获取item中EditText值
可以明确,现在没有直接方法可以获得ListView中每一行EditText的值. 解决方案:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...
- c# 调用 c++写的DLL
http://www.cnblogs.com/MarsPanda/archive/2012/09/03/2668522.html 解决办法 安装 vcredist 运行库 或者用VC6.0编写DLL ...
- memcached在windows下的安装与命令使用方法
先下载memcached for win32 下载地址1:http://filemarkets.com/fs/8tdo6ndg41d919599/ 下载地址2:http://www.400gb.com ...
- PAT 1010
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- 用eclipse javaEE编程时,不管什么程序都会出现这个错误[SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:bookstore' did not find
用eclipse javaEE编程时,不管什么程序都会出现这个错误[SetContextPropertiesRule]{Context} Setting property 'source' to 'o ...